代码应以以下方式工作:按下按钮 ->从数据库删除。
我试图从其他问题中遵循并复制答案,但没有工作解决方案。
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";
}
}
?>
您没有添加 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)。