使用AJAX + php删除行失败

时间:2016-12-03 16:07:18

标签: php jquery

代码应按以下方式工作:按下按钮 - >行从数据库中删除。

我试图关注并复制其他问题的答案,但没有可行的解决方案。

jquery代码:

$(document).on('click', ".menuRemove", function(event) {
  var del_h3name2 = $(this).parent().parent().prev().text();

  $.ajax({
    type:'POST',
    url:'deleteaccordion2.php',
    data:{'del_h3name2':del_h3name2},
    success: function(data){
      if (data=="YES") {
        alert("YES")
      } else {
        alert("can't delete the row")
      }
    }
  });
}

和php代码(deleteaccordion2.php):

<?php
  require 'database.php';

  if ( isset($_SESSION['user_id']) ) {

    $id = $_SESSION['user_id'];
    $accordion = $_POST['del_h3name2'];

    echo '$accordion';

    $delete = "DELETE FROM useraccordion WHERE id='$id', h3= '$accordion' ";
    $result = mysqli_query($delete);

    if ($result) {
      echo "YES";
    } else {
      echo "NO";
    }
  }
?>

1 个答案:

答案 0 :(得分:2)

您没有添加html所以我真的不知道您发送的值是否正确,但您的SQL语法确实有错误:

 DELETE FROM useraccordion WHERE id='$id', h3= '$accordion'
                                         ^ This is wrong.

您可以DELETE id = x AND h3 = y

$delete = "DELETE FROM useraccordion WHERE id='$id' AND h3= '$accordion' ";
  

请注意,您的代码容易受到SQL注入的攻击(请参阅boby tables)。