在 PHP 中调用 ajax 删除多行
Delete multiple rows with ajax call in PHP
我正在使用 Bootstrap Data Table 并且想从数据库中删除多个用户。我可以一次删除 1 个用户,没有问题,但是一旦我尝试删除多个用户,我 运行 遇到问题,我找不到任何错误。
这里是 AJAX 代码:
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 中的数据将是 userID=1,2
这是删除-user.php代码:
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();
}
}
当我检查多个用户时,第一个用户将被删除,但其他用户不会。我的代码有错误吗?
同样,我想要做的是删除多个用户,方法是从 table 中选择他们并传递包含多个 id 的隐藏输入的值,如 userID=1,2。当我直接转到 remove-user.php
页面并回显 GET 时,它显示为 1,2 无引号。如果我更改删除以指定 ID 而不是绑定参数,则一切正常。我真的不确定为什么它不起作用。
如果我需要提供更多信息,请告诉我。
问题是您如何将数据传递给 PDOStatement。
// assign :userID to $userID which should be cast into an int.
$stmt->bindParam(":userID", $userID, PDO::PARAM_INT);
这就是我可能采用的类似方法(假设您已经检查了适当的权限):
$ids_in = $_GET['userID'];
$ids_cast = array();
foreach(explode(',', $ids_in) as $id) {
// casting to an int means that SQL injection can't work, though I wonder if
// allowing a user to delete an arbitrary number of IDs is a good thing.
$ids_cast[] = intval($id);
}
// gets rid of bad strings &ct.
$ids_filtered = implode(',',array_filter($ids_cast));
if(!$ids_filtered) die('No valid IDs');
$query = "DELETE FROM users WHERE user_id IN ($ids_filtered)";
// run query.
在您的 SQL 查询中,:userID
参数是包含以逗号分隔的 ID 序列的字符串(例如,1,2
)。
$query = "DELETE FROM users WHERE user_id IN (:userID)";
但是在绑定时,您将参数定义为在 bindParam
函数中传递 PDO::PARAM_INT
参数的整数。
$stmt->bindParam(":userID", $userID, PDO::PARAM_INT);
尝试使用
$stmt->bindParam(":userID", $userID, PDO::PARAM_STR);
相反。
所以我终于找到了一个我认为我尝试过的解决方案。我认为它不起作用的原因可能与我尝试使用 bindParam
.
有关
这是我将 remove-user.php
代码更改为:
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;
}
我正在使用 Bootstrap Data Table 并且想从数据库中删除多个用户。我可以一次删除 1 个用户,没有问题,但是一旦我尝试删除多个用户,我 运行 遇到问题,我找不到任何错误。
这里是 AJAX 代码:
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 中的数据将是 userID=1,2
这是删除-user.php代码:
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();
}
}
当我检查多个用户时,第一个用户将被删除,但其他用户不会。我的代码有错误吗?
同样,我想要做的是删除多个用户,方法是从 table 中选择他们并传递包含多个 id 的隐藏输入的值,如 userID=1,2。当我直接转到 remove-user.php
页面并回显 GET 时,它显示为 1,2 无引号。如果我更改删除以指定 ID 而不是绑定参数,则一切正常。我真的不确定为什么它不起作用。
如果我需要提供更多信息,请告诉我。
问题是您如何将数据传递给 PDOStatement。
// assign :userID to $userID which should be cast into an int.
$stmt->bindParam(":userID", $userID, PDO::PARAM_INT);
这就是我可能采用的类似方法(假设您已经检查了适当的权限):
$ids_in = $_GET['userID'];
$ids_cast = array();
foreach(explode(',', $ids_in) as $id) {
// casting to an int means that SQL injection can't work, though I wonder if
// allowing a user to delete an arbitrary number of IDs is a good thing.
$ids_cast[] = intval($id);
}
// gets rid of bad strings &ct.
$ids_filtered = implode(',',array_filter($ids_cast));
if(!$ids_filtered) die('No valid IDs');
$query = "DELETE FROM users WHERE user_id IN ($ids_filtered)";
// run query.
在您的 SQL 查询中,:userID
参数是包含以逗号分隔的 ID 序列的字符串(例如,1,2
)。
$query = "DELETE FROM users WHERE user_id IN (:userID)";
但是在绑定时,您将参数定义为在 bindParam
函数中传递 PDO::PARAM_INT
参数的整数。
$stmt->bindParam(":userID", $userID, PDO::PARAM_INT);
尝试使用
$stmt->bindParam(":userID", $userID, PDO::PARAM_STR);
相反。
所以我终于找到了一个我认为我尝试过的解决方案。我认为它不起作用的原因可能与我尝试使用 bindParam
.
这是我将 remove-user.php
代码更改为:
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;
}