我有2个文件,1)表格2)更新文件
这是更新表单update.php:
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="datasename"; // Database name
$tbl_name="data"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// 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);
?>
<div align="center"><img src="http://www.multihunters.co.uk/images/tf_ideal/blue/misc/logo.png" width="400" height="125">
</div>
<div align="center">
<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>Citizen ID</strong></td>
<td align="center"><strong>Citizen Name</strong></td>
<td align="center"><strong>Citizenship</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="citid" type="text" id="citid" value="<? echo $rows['citid']; ?>">
</td>
<td align="center">
<input name="citname" type="text" id="citname" value="<? echo $rows['citname']; ?>" size="15">
</td>
<td>
<input name="citizenship" type="text" id="citizenship" value="<? echo $rows['citizenship']; ?>" size="15">
</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>
</div>
<?php
// close connection
mysql_close();
?>
这是我的行动表格update_ac.php:
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="data"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET citid='$citid', citname='$citname', citizenship='$citizenship' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='http://www.multihunters.co.uk/citizens_permlist.php'>View result</a>";
}
else
{
echo "ERROR";
}
消息成功出现,但当我检查数据是否已更新时,没有任何改变,我做错了什么?
答案 0 :(得分:2)
您没有定义$citid
,$citname
,$citizenship
和$id
。您必须从$_POST
获取它,将其转义,然后将其放入查询中。
另外,请停止使用mysql_*
API。使用mysqli
或PDO扩展程序。
答案 1 :(得分:0)
只需将代码更改为
即可$citid = mysql_real_escape_string($_POST['citid']);
$citname = mysql_real_escape_string($_POST['citname']);
$citizenship = mysql_real_escape_string($_POST['citizenship']);
$sql="UPDATE $tbl_name SET citid='$citid', citname='$citname', citizenship='$citizenship' WHERE id='$id'";
$result=mysql_query($sql);
注意强>
从PHP 5.5.0起,不推荐使用以mysql_
开头的PHP函数。如果您有能力这样做,请考虑更新您的代码,以改为使用MySQLi或PDO扩展程序。