我试图在crud操作中执行delete操作,但当我单击delete按钮时,似乎什么都不起作用:每次单击delete按钮时onclick事件都没有响应。
我什么都试过了,但似乎都没用。
以下是源代码:
html格式
<!-- remove modal -->
<div class="modal fade" tabindex="-1" role="dialog" id="removeDeptModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><span class="glyphicon glyphicon-trash"></span> Remove Member</h4>
</div>
<div class="modal-body">
<p>Do you really want to delete this department ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="removeBtn"><span class="glyphicon glyphicon-trash"></span> Delete</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- /remove modal -->
php代码
检索.php
<?php
require_once('../../includes/config.php');
$output = array('data' => array());
$sql = "SELECT * FROM department order by id desc";
$query = $mysqli->query($sql);
$x = 1;
while ($row = $query->fetch_assoc()) {
$actionButton = '
<a type="button" data-toggle="modal" data-target="#editMemberModal" class="btn btn-info btn-sm" onclick="editMember('.$row['id'].')"> <span class="glyphicon glyphicon-edit"></span></a>
<a type="button" data-toggle="modal" data-target="#removeDeptModal" class="btn btn-danger btn-sm" onclick="removeMember('.$row['id'].')"> <span class="glyphicon glyphicon-trash"></span></a>';
$output['data'][] = array(
$x,
$row['dept_code'],
$row['dept_name'],
$actionButton
);
$x++;
}
// database connection close
$mysqli->close();
echo json_encode($output);
删除.php
<?php
require_once('../../includes/config.php');
$output = array('success' => false, 'messages' => array());
$memberId = $_POST['member_id'];
$sql = "DELETE FROM department WHERE id = {$memberId}";
$query = $mysqli->query($sql);
if($query === TRUE) {
$output['success'] = true;
$output['messages'] = 'Successfully Deleted';
} else {
$output['success'] = false;
$output['messages'] = 'Error while deleting the Department information';
}
// close database connection
$mysqli->close();
echo json_encode($output);
?>
最后,jquery代码:
function removeMember(id = null) {
if(id) {
// click on remove button
$("#removeBtn").unbind('click').bind('click', function() {
$.ajax({
url: 'dept/remove.php',
type: 'post',
data: {member_id : id},
dataType: 'json',
success:function(response) {
if(response.success == true) {
$(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'
'<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+'</div>');
// refresh the table
manageDeptTable.ajax.reload(null, false);
// close the modal
$("#removeDeptModal").modal('hide');
} else {
$(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+ '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.messages+
'</div>');
}
}
});
}); // click remove btn
} else {
alert('Error: Refresh the page again');
}
}
1条答案
按热度按时间xbp102n01#
问题在于你自己
DELETE
sql语言,应该是
另外,我建议改用preparedstatement,以避免任何sql注入和语法错误。