-1

我正在使用 ajax 方法调用 PHP 脚本,该脚本应在单击 ID 为“删除”的按钮时从我的数据库中删除一行。

$('#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返回成功,但是查询没有被执行。

如果有人可以帮我解决这个问题,我将非常感激..

4

1 回答 1

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
  }    
});
于 2013-10-09T21:11:09.930 回答