我已经创建了一个HTML表单,该表单处理数据并将其存储到MySQL数据库中,然后尝试从网页更新显示该信息。更新和显示MySQL表格条目
我能够在PhpMyAdmin之外正确显示我的表单输入数据,但每当我点击更新时,更新都不会反映在网页或MySQL数据库中。
这里是我的代码:
<html>
<body>
<?php
$link = mysql_connect("localhost","hci","boohoo");
if(!$link) {
die('Connection cannot be made: ' . mysql_error());
}
mysql_select_db("test5", $link);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE webform SET Name='$_POST[name]', Email=$_POST[email]'
, Website ='$_POST[website], Comment ='$_POST[comment], Gender
='$_POST[gender]' WHERE Topic='$_POST[hidden]'";
mysql_query($UpdateQuery, $link);
};
$sql = "SELECT * FROM webform";
$myData = mysql_query ($sql, $link);
echo"<table border=1>
<tr>
<th>Name</th>
<th>Email</th>
<th>Website</th>
<th>Comment</th>
<th>Gender</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=mydata3.php method=POST>";
echo "<tr>";
echo "<td>" . "<input type=text name=name value=" . $record['name'] . "
</td>";
echo "<td>" . "<input type=text name=email value=" . $record['email'] . "
</td>";
echo "<td>" . "<input type=text name=website value=" . $record['website'] .
" </td>";
echo "<td>" . "<input type=text name=comment value=" . $record['comment'] .
" </td>";
echo "<td>" . "<input type=text name=gender value=" . $record['gender'] . "
</td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['email'] . "
</td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</form>";
}
echo "</table>";
mysql_close($link);
?>
</body>
</html>
**警告**:如果您只是学习PHP,请不要使用['mysql_query'](http://php.net/manual/en/function.mysql-query.php)接口。这是非常可怕和危险的,它在PHP 7中被删除了。[PDO的替代品并不难学](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南介绍了最佳实践。你的用户数据是**不是** [正确转义](http://bobby-tables.com/php.html),并有[SQL注入漏洞](http://bobby-tables.com/),并且可以被利用。 – tadman
1.将'mysql_query($ UpdateQuery,$ link);'改为:'mysql_query($ UpdateQuery,$ link)或die(mysql_error());'2.选择任何框架,例如:https://codecondo.com/minimal-web-frameworks-for-php/ – bato3
mysql_query($ UpdateQuery,$ link)完全不起作用,die(mysql_error());导致webform在更新时消失并且没有更新SQL数据库。 –