我已經創建了一個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數據庫。 –