问题描述
我使用引导数据表,并希望从数据库中删除多个用户。我能够删除1用户在同一时间使用没有问题,但一旦我尝试删除多个我遇到问题,我找不到任何错误。
I am using Bootstrap Data Table and want to delete multiple users from the database. I am able to delete 1 user at a time with no problem but once I try to delete more than one I run into issues and I cannot find any errors.
下面是AJAX code:
Here is the AJAX code:
function removeRow(){
var url = 'remove-user.php';
var id = document.getElementById("user-id").value;
var data = 'userID=' + id;
$.ajax({
url: url,
data: data,
cache: false,
error: function(e){
alert(e);
},
success: function () {
alert(data);
var selects = $('#users-table').bootstrapTable('getSelections');
ids = $.map(selects, function (row) {
return row.id;
});
$('#users-table').bootstrapTable('remove', {
field: 'id',
values: ids
});
}
});
}
例如:在URL中的数据将用户ID = 1,2
Example: The data in the url will be userID=1,2
下面是删除,user.php的code:
Here is the remove-user.php code:
require("../config.php");
if(isset($_GET['userID'])) {
try{
$userID = $_GET['userID'];
$query = "DELETE FROM users WHERE user_id IN (:userID)";
$stmt = $db->prepare($query);
$stmt->bindParam(":userID", $userID, PDO::PARAM_INT);
$stmt->execute();
$user_removed = 'User was successfully deleted.';
$_SESSION['user_removed'] = $user_removed;
} catch (Exception $e){
echo 'The following error occured: <br/>'.$e->getMessage();
}
}
当我检查多个用户第一用户将得到删除,而不是其他。有没有在我的code什么错误?
When I check more than one user the first user will get delete, but not the others. Are there any mistakes in my code?
此外,我所希望做的是通过从表中选择它们,并通过一个隐藏的输入,其中包含多个ID这样的值删除多个用户 - 用户ID = 1,2。当我直接去删除-user.php的
页,并呼应得到它显示为1,2无报价。如果我改变了我的绑定删除指定的ID,而不是一个参数一切正常。我真的不知道为什么它不工作。
Again, what I am looking to do is delete multiple users by selecting them from the table and passing the value of a hidden input which contains multiple ids like this - userID=1,2. When I go to the remove-user.php
page directly and echo the GET it displays as 1,2 no quotes. If I change my delete to specify the IDs instead of binding a parameter everything works fine. I'm really not sure why its not working.
请让我知道如果我需要提供更多的信息。
Please let me know if I need to give more info.
推荐答案
所以,我终于能找到,我想我尝试了解决方案。我想,为什么不工作可能有一些与我试图用其原因 bindParam
。
So I was finally able to find a solution which I thought I tried. I think the reason why it wasn't working might have something to do with me trying to use bindParam
.
下面是我改变了我的删除-user.php的
code到:
Here is what I changed my remove-user.php
code to:
try{
$ids = array($_GET['userID']);
$inQuery = implode(',', $ids);
$stmt = $db->prepare(
'DELETE
FROM users
WHERE user_id IN(' . $inQuery . ')'
);
$stmt->execute($ids);
$count = $stmt->rowCount();
$user_removed = ''.$count.' user(s) deleted successfully.';
$_SESSION['user_removed'] = $user_removed;
} catch (Exception $e){
$error = '<strong>The following error occured:</strong>'.$e->getMessage();
$_SESSION['error'] = $error;
}
这篇关于删除多行,在PHP Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!