-1

ID「削除」のボタンをクリックすると、データベースから行を削除するPHPスクリプトを呼び出すためにajaxメソッドを使用しています。

$('#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 に答える