gpt4 book ai didi

javascript - 删除整个数据库而不是一行

转载 作者:行者123 更新时间:2023-11-30 21:50:30 26 4
gpt4 key购买 nike

我一直在尝试通过单击删除按钮来删除我的 mySQL 数据库中的一行。但是不是删除一个 mySQL 行,而是删除数据库中的所有行。

我只针对特定的 ID,所以我不清楚为什么所有其他 ID 都被删除了。

HTML:

<?php foreach ($movies as $movie) : ?>
<div class="col-4">
<div class="card card-cascade">
<div class="view gradient-card-header purple-gradient">
<h2><?php echo $movie['name']; ?></h2>
<p><?php echo $movie['genre']; ?></p>
</div>
<div class="card-body text-center">

<!-- Delete -->
<a type="button" class="btn-floating btn-small btn-dribbble delbutton" data-toggle="tooltip" data-placement="top" title="Delete" id="<?php echo $movie['id']; ?>"><i class="fa fa-trash-o" aria-hidden="true"></i></a>

</div>
</div>
</div>
<?php endforeach; ?>

JS:

$(function () {
// Tooltips Initialization
$('[data-toggle="tooltip"]').tooltip();

// Delete Movie
$(".delbutton").click(function() {
console.log('watch me')
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
$.ajax({
type : "POST",
url : "../movieApp/delete.php", //URL to the delete php script
data : {id:info},
success : function() {
console.log("success");
},
error: function () {
console.log("failed");
},
});
$(this).parents(".record").animate("fast").animate({
opacity : "hide"
}, "slow");
}
return false;
});
});

PHP:

require 'config/config.php';
require 'config/db.php';

if($_POST['id']){
$id=$_POST['id'];

$delete = "DELETE FROM movies WHERE id=$id";
$result = $conn->query($delete);
}

if (mysqli_query($conn, $sql)) {
mysqli_free_result($result);
mysqli_close($conn);
echo "Worked!";
exit;
} else {
echo "Error deleting record";
}

最佳答案

您设置了ajax方法POST,但Post数据格式不符合您的要求。

Change your ajax Data like as

 //var info = 'id=' + del_id;
var info = {
id : del_id
}

And

$.ajax({
/*...*/
data : info,
/*.../
});

And also check if your id field is string, If integer then change the Query string to -

#$delete = "DELETE FROM movies WHERE id='$id'";
$delete = "DELETE FROM movies WHERE id=$id";

Also change -

#$_POST['info']
$_POST['id']

因为,您没有在代码中的任何地方设置 $_POST['info']

注意:并且不要忘记console您正确的Ajax URL

关于javascript - 删除整个数据库而不是一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com