無法將新數據更新到表中。無法使用PHP將數據更新到MySQL中
我的表結構:
Table book:
-id (Primary, Auto Increment, INT)
-title
-publisher_id (Foreign key to Table publisher.pub_id, INT)
Table publisher:
-pub_id (Primary, Auto Increment, INT)
-pub_name
我想要做的就是更新新的數據到桌上的書,但不能。 我找不到錯在哪裏。
代碼:
<?php
if($_POST){
$uid = $_GET['id'];
$title = mysqli_real_escape_string($mysqli, $_POST['title']);
$publisher = mysqli_real_escape_string($mysqli, $_POST['publisher']);
// Update publisher
$queryid = "SELECT pub_id FROM publisher WHERE pub_name = '$publisher' ";
$result = $mysqli->query($queryid);
$pub_id = $result->fetch_assoc();
//4Update book
$query = "UPDATE book
SET
title = '$title',
publisher_id = 1
WHERE id = $uid";
$mysqli->query($query) or die();
$msg ='bookinfo updated';
header('Location: index.php?msg=' . urlencode($msg) . '');
exit;
}
?>
當我改變'$pub_id['pub_id']'
到'".$pub_id['pub_id']."'
, 「去哪兒」 行變成黃色喜歡的圖片,...
改變這樣的代碼沒工作:
$query = "UPDATE book
SET
title='$title',
publisher_id= 1
WHERE id= $uid";
什麼了你的日誌FIL e告訴你 – Webdesigner
@Webdesigner對不起,我是PHP新手,你的意思是錯誤信息還是別的?運行代碼後沒有錯誤消息.. – Rufus7
像@Webdesigner說的那樣,檢查你的日誌。此外,你可以[** SQL注入**](https://www.owasp.org/index.php/SQL_Injection)。您需要使用準備好的語句,而不是將變量連接到查詢中。請參閱[如何防止PHP中的SQL注入?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1)。 –