目标
删除选择了ID的所有数据
问题
数据不会被删除
错误
在开发者控制台中没有提示错误
工作说明
根据我的理解,代码的功能是检索用户ID(delete_userID
),然后使用此行$delete_userID
将其传递到mysqli_real_escape_string($conn,$_POST['delete_userID']);
。但我无法理解为什么删除查询不起作用。我已经查看了几个有关此问题的在线问题,其中一些问题是因为缺少数据库代码mysql_select_db("data2017", $conn);
,但这对我来说似乎不是问题。此外,我将其设置为单击输入按钮后,它将发送"删除"如果已单击按钮并且已收到呼叫,则调用if
语句。除了检索用户ID之外,还要通过使用JavaScript函数传递要删除的用户ID,如下所示。
删除代码
<div class="modal-body">
<p>Do you want to delete?</p>
</div>
<div class="form-group">
<label for="delete_userID" class="control-label">User ID:</label>
<input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
<input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
<?php
if(isset($_POST['delete'])){
$delete_userID=mysqli_real_escape_string($conn,$_POST['delete_userID']);
mysql_select_db("data2017", $conn);
$sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
$Deletequery=mysqli_query($conn,$sql);
if($Deletequery){
echo "<script>alert('Delete Successful.');</script>";
}
else{
echo "<script>alert('Delete Failed.');</script>";
}
}
?>
</div>
更新
delete_userID是通过JavaScript函数传递的,如下所示。
function bindList() {
var $table = $('#eventsTable');
$table.bootstrapTable({
url: 'list-phaUser.php',
search: true,
pagination: true,
columns: [{
field: 'userID',
title: 'User ID',
sortable: true,
},{
field: 'operate',
title: 'Action',
align: 'center',
sortable: true,
events: operateEvents,
formatter: operateFormatter
},],
});
$(".fixed-table-toolbar").append("<div id='table-title' class='columns columns-left btn-group pull-left'>User List</div>");
$("#divFooter").remove();
$("<div class='row' id='divFooter' style='margin-left:-4%;margin-right:-4%;margin-bottom:-2%;'></div>").insertAfter("#divtb1");
$("#divFooter").load('footer.php');
}
window.operateEvents = {
'click .icon_edit': function (e, value, row, index) {
$("#edit_userID").val(row.userID);
$('#edit_model').modal('show');
},
'click .icon_delete': function (e, value, row, index) {
$("#delete_userID").val(row.userID);
$('#delete_model').modal('show');
}
};
答案 0 :(得分:3)
你没有制作表格。
<div class="modal-body">
<p>Do you want to delete?</p>
</div>
<form method="POST" action="">
<div class="form-group">
<label for="delete_userID" class="control-label">User ID:</label>
<input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
<input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
</div>
</form>
<?php
if (isset($_POST['delete'])) {
$delete_userID = mysqli_real_escape_string($conn,$_POST['delete_userID']);
mysql_select_db("data2017", $conn);
$sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
$Deletequery = mysqli_query($conn,$sql);
if ($Deletequery) {
echo "<script>alert('Delete Successful.');</script>";
} else {
echo "<script>alert('Delete Failed.');</script>";
}
}
?>
答案 1 :(得分:1)
尝试使用PDO,因为最新的PHP版本不推荐使用MySQL DB连接。 首先,您必须声明表单方法和操作,然后您还必须关闭它。
请尝试以下代码。
<?php
$config = array(
'host' => 'localhost',
'username' => 'db_username',
'password' => 'db_password',
'dbname' => 'data2017'
);
$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['delete_userID'])){
$delete_userID = $_POST['delete_userID'];
// No need to use mysqli_real_escape_string when we using prepared statements
$stmt = $db->prepare("DELETE FROM pha_user WHERE id = '$delete_userID' ");
$stmt->execute();
if($stmt){
header("Location:appropriate_page.php?del_success");
}else{
header("Location:appropriate_page.php?del_failure");
}
}
?>
<form method="POST" action="">
<div class="form-group">
<label for="delete_userID" class="control-label">User ID:</label>
<input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
<input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
</div>
</form>