如果我想要删除记录后我console.log
ID,则在控制台中显示正确的ID。因此,似乎它已正确连接但实际上并未从我的数据库中删除记录。我无法弄清楚这一点,但它可能是显而易见的。
JS
<script type="text/javascript">
$(document).ready(function() {
$('.table-row').on('click', function (e) {
e.preventDefault();
var $otId = $(this).data('ot-id');
swal({
title: 'Are you sure?',
text: "You won't be able to undo this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#FB404B',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
closeOnCancel: false
}, function (isConfirm) {
if (isConfirm) {
$.ajax({
type: 'post',
url: 'functions/delete-ot-request.php',
data: {otId: $otId},
success: function (result) {
// window.location.href = 'delete-ot.php';
console.log($otId);
},
});
} else {
swal("Cancelled", "Maybe next time then.", "error");
}
});
});
});
</script>
PHP
if(isset($_POST['otId'])) {
$stmt = $link->prepare("DELETE FROM `ot_request` WHERE `id` = ? LIMIT 1");
$stmt->bind_param("i", $_POST['otId']);
$stmt->execute();
$stmt->close();
}
这是包含id:
的html表行<tr class="table-row" data-ot-id="{$id}"></tr>
答案 0 :(得分:0)
您的console.log($otId);
报告您之前发送的ID值,而不是从服务器端返回的数据。
因此,此输出仅证明 ajax进程成功,而不是服务器作业,这可能由于某种原因而出现错误。
所以你应该在服务器端查找错误,从发回一些执行跟踪开始:
if(isset($_POST['otId'])) {
try {
$stmt = $link->prepare("DELETE FROM `ot_request` WHERE `id` = ? LIMIT 1");
$stmt->bind_param("i", $_POST['otId']);
$stmt->execute();
$stmt->close();
echo 'otId "' . $_POST['otId'] . '" processed';
} catch(PDOException $e) {
echo $e->getMessage;
}
} else {
echo 'No otId found';
}
修改您的Javascript,以便输出此跟踪:
success: function (result) {
console.log(result);
}