我使用以下代码从数据库中删除数据。
<?php
//include database connection
include 'connect_db.php';
try {
$query = 'DELETE FROM tasks WHERE task_id = ?';
$stmt = $DBH->prepare($query);
$stmt->bindParam(1, $_POST['task_id']);
var_dump($stmt);
if($stmt->execute()){
echo "Task was deleted.";
}else{
echo "Unable to delete Task.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
但是,如果我使用以下URL调用此文件,我将获得以下内容。 数据无法删除。
~delete.php?task_id=9
object(PDOStatement)#2 (1) { ["queryString"]=> string(35) "DELETE FROM tasks WHERE task_id = ?" } Task was deleted.
Ajax功能 我使用以下ajax函数来删除数据。 //单击DELETE按钮时
$(document).on('click', '#deleteBtn', function(){
if(confirm('Are you sure?')){
// get the id
var task_id = $(this).closest('td').find('#task_id').text();
// trigger the delete file
$.post("delete.php", { id: task_id })
.done(function(data) {
// you can see your console to verify if record was deleted
console.log(data);
$('#loaderImage').show();
// reload the list
showTasks();
});
}
});