【发布时间】:2012-06-03 14:02:43
【问题描述】:
我正在尝试更新具有 5 个字段的表中的数据。 字段如下:proj_name、cust_name、address、cost 和 details。
这是我的更新代码(update_orde.php):
<?php
include("connect.php");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM project 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_orde_suc.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td colspan="5"><strong>Update Data</strong> </td>
</tr>
<tr>
<td align="center"><strong>Project Name</strong></td>
<td align="center"><strong>Customer</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>Cost</strong></td>
<td align="center"><strong>Details</strong></td>
</tr>
<tr>
<td><input name="pn" type="text" id="pn" value="<? echo $rows['proj_name']; ?>"></td>
<td align="center"><input name="cn" type="text" id="cn" value="<? echo $rows['cust_name']; ?>" size="15"></td>
<td align="center"><input name="add" type="text" id="add" value="<? echo $rows['address']; ?>" size="15"></td>
<td><input name="cost" type="text" id="cost" value="<? echo $rows['cost']; ?>" size="15"></td>
<td><input name="details" type="text" id="details" value="<? echo $rows['details']; ?>" size="15"></td>
</tr>
<tr>
<td align="center" colspan="5"><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> <input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// close connection
mysql_close();
?>
这是成功插入数据库(update_orde_suc.php):
<?php
include("connect.php");
$id=$_POST['id'];
$pn = $_POST['proj_name'];
$cn = $_POST['cust_name'];
$add = $_POST['address'];
$cost = $_POST['cost'];
$det = $_POST['details'];
// update data in mysql database
$sql="UPDATE project SET proj_name='$pn', cust_name='$cn',address='$add', cost='$cost', details='$det' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='orders_edit.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
问题是,当我尝试更改数据时,出现一个错误,提示 update_orde_suc.php 的 3 个第一个索引未定义(成本和细节都可以)。
最奇怪的是,我在另一个表更新中使用了完全相同的代码,它工作得很好,我现在唯一要做的就是更改变量的名称以对应新表的名称。
【问题讨论】:
-
在我看来,您的 SQL 可用于注入。在unixwiz.net/techtips/sql-injection.html 或其他网站阅读一些相关信息
-
您在表单中调用字段
pn、cn和add很自然,proj_name、cust_name和address不存在。 -
在这种情况下,您应该做的第一件事是添加
print_r( $_POST )并检查表单实际发送的内容。 -
前 3 个变量没有给出任何值...但是成本和详细信息返回插入到表中的值。也许问题出在 update_orde.php 那么?我应该再调查一下!
标签: php mysql html-table undefined-index