我正在使用ajax方法来调用PHP脚本,该脚本应该在单击id为“delete”的按钮时从我的数据库中删除一行。
$('#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
所以ajax成功返回,但查询没有被执行。
如果有人可以帮我解决这个问题,我会非常感激..
答案 0 :(得分:4)
您没有在AJAX通话中正确设置数据。这样做:
$.ajax({
url : './php/delete.php',
type: 'POST',
data : {id:id}, // use a map like this to set up the POST data
success: function()
{
alert('success deleting ' + id);
location.reload();
// These both get called correctly, but the row is not actually getting deleted
}
});
答案 1 :(得分:-3)
您的查询不对。你没有逃脱字符串。你必须这样做: 您也没有将变量连接到字符串。
mysqli_query($con,"DELETE FROM htmlcontent WHERE id='".$_POST[id]."'")