我想添加我的網站的「管理」部分。現在我正在開發一個部分,爲我的MySQL數據庫添加一個新行。如何使用PHP爲mySQL添加一行?
第一個文件是我的admin.php的:
<html>
...
<body>
<form action="add.php" method="post">
<input type="text" name="order" />
<input type="text" name="newstatus" />
<input type="submit" value="Add" />
</form>
</body>
</html>
我在這裏的目標是2個數據(表只有兩列現在)添加到新行。
然後,我有我的add.php文件:
<?
//declare my connection variables - I'll move these to a secure method later
mysql_connect($servername, $username, $password) or die (mysql_error());
// Select database
mysql_select_db($dbname) or die(mysql_error());
// The SQL statement is built
$sql="INSERT INTO $tblname(id, status)VALUES('$order', '$newstatus')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='admin.php'>Back to main page</a>";
}
else {
echo mysqli_errno($this->db_link);
}
?>
<?php
// close connection
mysql_close();
?>
每當我輸入的任何數據(的什麼已經是表中的非重複的),它給了我一個錯誤。如果我完全清除了所有數據的表格,則會輸入一次。我得到一個重複鍵錯誤,但我的密鑰應該是「訂單」,每次輸入時都是唯一的。
對此提出建議?
建議刪除mysql_ *函數並切換到pdo,並使用預處理語句。如果你有興趣找出原因,搜索'sql injection' – 2015-04-04 18:10:44