我正在使用ajax方法來調用一個PHP腳本,該腳本應該在點擊一個ID爲'delete'的按鈕後從我的數據庫中刪除一行。MySQL DELETE在PHP中不執行查詢
$('#delete').click(function() {
var id = parseInt($('#content').attr('data-name'));
// The row in my database has the type integer
// Doing alert(id) correctly returns the value
$.ajax({
url : './php/delete.php',
type: 'POST',
data : id,
success: function()
{
alert('success deleting ' + id);
location.reload();
// These both get called correctly, but the row is not actually getting deleted
}
});
});
我的PHP代碼是
<?php
$con=mysqli_connect("localhost","root","tulula15","content_test");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM htmlcontent WHERE id='$_POST[id]'");
mysqli_close($con);
?>
運行在phpMyAdmin同一查詢正確地刪除了該行
DELETE FROM htmlcontent WHERE id=2
所以阿賈克斯成功返回,但沒有被執行的查詢。
如果有人可以幫助我解決這個問題我會非常感激..
您極易受SQL注入影響。 –
1.運行查詢時使用mysqli錯誤處理,並查看服務器日誌文件。 – arkascha
使用prepare語句來參數化$ _POST ['id']變量 – aarryy