gpt4 book ai didi

php - 更新表格不起作用

转载 作者:行者123 更新时间:2023-11-29 08:31:23 25 4
gpt4 key购买 nike

我有 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>&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>Citizen ID</strong></td>
<td align="center"><strong>Citizen Name</strong></td>
<td align="center"><strong>Citizenship</strong></td>
</tr>
<tr>
<td>&nbsp;</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>&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>
</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";
}

出现消息成功,但当我检查数据是否已更新时没有任何变化,我做错了什么?

最佳答案

您没有定义 $citid$citname$citizenship$id。您必须从 $_POST 获取它,对其进行转义,然后将其放入查询中。

另外,请停止使用mysql_* API。使用 mysqli 或 PDO 扩展。

关于php - 更新表格不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16496105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com