我已经玩了好几个小时,但无济于事,我一直在构建一个脚本,该脚本将允许我通过在表中进行编辑来在管理员的网页上编辑信息。我使用了一些教程等,但是现在它只是不想更新数据库。
我有三个要素:
第一:列表列表-工作正常
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> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </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> </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> </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> </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";
}
?>
关于如何使它起作用的任何想法或建议吗?提前致谢!!
这里的问题是,您正在呈现一个表单来更新多个条目,所有条目都共享具有相同id
HTML属性的属性。您的更新脚本无法区分具有相同的DOM元素id
,因此,即使您通过将传递id
为隐藏属性来标识每个条目,也无法正确更新记录。
一种可能的解决方案是将条目的唯一ID作为该条目的每个属性的一部分传递id
:
<input name="Price per Entry" type="text" id="cpe1_<? echo $rows['id']; ?>" value="<? echo $rows['cpe1']; ?>">
这id
将为每个表单字段创建不同的元素。然后,在更新脚本中,您可以将条目的属性附加id
到elementid
属性,以便它正确地将条目的属性与正确的表单字段相关联。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句