我查看了我的查詢語句。當我在MYSQL中運行「DELETE FROM users WHERE id =(Some Int)」時,它成功執行並且一切似乎都正常。不過,我在MYSQL出現語法錯誤附近「當我嘗試執行此相同的命令PHP MYSQL刪除語法錯誤
include 'db_connect.php'; // connects to db
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
//execute query
if($mysqli->query($query)){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
$disp = mysqli_error($mysqli);
print($disp);
}
編輯1: 當我打印出$查詢,我得到:‘DELETE FROM用戶WHERE ID =’ 。 ID爲空
我試圖改變$查詢到以下內容:。
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id']);// prints nothing
$query = "DELETE FROM users WHERE id = ".$mysqli->$_GET['id'];// prints nothing
$query = "DELETE FROM users WHERE id = ".$mysqli->$id;// prints nothing
我還是不明白,爲什麼我不能在這裏使用的$ id 我調用使用ID和它的JavaScript函數工作正常...我得到一個彈出窗口並在其中打印正確的ID。
echo "<a href='#' onclick='delete_user({$id});'>Delete</a>";
function delete_user(id){
var answer = confirm('Are you sure?'+id);
if (answer){ //if user clicked ok
window.location = 'delete.php?action=delete&id=' + id;
}
}
'$ query =「DELETE FROM users WHERE id =」。$ mysqli-> real_escape_string($ _ GET ['id']);' – aldrin27
'_real_escape_string'對數字值沒有任何作用。它意味着**串**。所以如果你沒有用引號包裝價值,那麼「逃避」是毫無意義的。 (請閱讀SQL注入,然後參數綁定,這樣可以避免這種誤用) – mario
echo'$ query'或者檢查'$ _GET ['id']'爲空或有一些值 –