PHP SCRIPT - 执行时不更新数据库

时间:2014-01-11 01:23:10

标签: php mysql sql

我已经玩了好几个小时,但无济于事,我一直在构建一个脚本,允许我通过在表格中编辑来管理管理员网页上的信息。我使用了一些教程等,但现在它只是不想更新数据库。

我有三个要素:

第一个:列表 - 工作正常

require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");


 // Connects to your Database 
 mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error()); 
 mysql_select_db("cl52-abcdef") or die(mysql_error()); 


$tbl_name="DealOne";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>


<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Desc</strong></td>
<td align="center"><strong>Dest</strong></td>
<td align="center"><strong>RRP</strong></td>
<td align="center"><strong>Price Entry</strong></td>
<td align="center"><strong>Entries Avail</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><? echo $rows['holdesc1']; ?></td>
<td><? echo $rows['holdest1']; ?></td>
<td><? echo $rows['rrp1']; ?></td>
<td><? echo $rows['cpe1']; ?></td>
<td><? echo $rows['ea1']; ?></td>

<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>

<?php
mysql_close();
?>

第二个:表单更新数据的位置

require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");


 // Connects to your Database 
 mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error()); 
 mysql_select_db("cl52-abcdef") or die(mysql_error()); 


$tbl_name="DealOne";


// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Desc</strong></td>
<td align="center"><strong>Dest</strong></td>
<td align="center"><strong>RRP</strong></td>
<td align="center"><strong>Price Entry</strong></td>
<td align="center"><strong>Entries Avail</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="desc" type="text" id="holdesc1" value="<? echo $rows['holdesc1']; ?>" size="35">
</td>
<td align="center">
<input name="Destination" type="text" id="holdest1" value="<? echo $rows['holdest1']; ?>" size="35">
</td>
<td>
<input name="RRP" type="text" id="rrp1" value="<? echo $rows['rrp1']; ?>" size="8">
</td>
<td align="center">
<input name="Price per Entry" type="text" id="cpe1" value="<? echo $rows['cpe1']; ?>">
</td>
<td align="center">
<input name="Entries Available" type="text" id="ea1" value="<? echo $rows['ea1']; ?>" size="8">
</td>


</tr>
<tr>
<td>&nbsp;</td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php
// close connection 
mysql_close();
?>

最后第三个,我相信会更新数据库,但它不会:

require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");


 // Connects to your Database 
 mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error()); 
 mysql_select_db("cl52-abcdef") or die(mysql_error()); 


$tbl_name="DealOne";

// update data in mysql database 
$sql="UPDATE $tbl_name SET holdesc1='$holdesc1', holdest1='$holdest1', rrp1='$rrp1', cpe1='$cpe1', ea1='$ea1' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='view_posts.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

有关如何使其发挥作用的任何想法或建议?在此先感谢!!

2 个答案:

答案 0 :(得分:1)

您的$ tbl_name未引用。

你有

$sql="UPDATE $tbl_name SET holdesc1='$holdesc1', holdest1='$holdest1', rrp1='$rrp1', cpe1='$cpe1', ea1='$ea1' WHERE id='$id'";

我认为应该是:

$sql="UPDATE '$tbl_name' SET holdesc1='$holdesc1', holdest1='$holdest1', rrp1='$rrp1', cpe1='$cpe1', ea1='$ea1' WHERE id='$id'";

答案 1 :(得分:0)

这里的问题是您要渲染单个表单来更新多个条目,所有条目共享属性具有相同的id HTML属性。您的更新脚本无法区分具有相同id的DOM元素,因此,您的记录将无法正确更新 - 即使您通过将id作为隐藏属性传递来识别每个条目

一种可能的解决方案是将条目的唯一ID作为每个条目属性“id”的一部分传递:

<input name="Price per Entry" type="text" id="cpe1_<? echo $rows['id']; ?>" value="<? echo $rows['cpe1']; ?>">

这会为每个表单字段创建不同的id元素。然后,在更新脚本中,您可以将条目的id附加到元素id属性,以便正确地将条目的属性与正确的表单字段相关联。