簡體   English   中英

在PHP中使用Ajax調用刪除多行

[英]Delete multiple rows with ajax call in PHP

我正在使用Bootstrap數據表,並想從數據庫中刪除多個用戶。 我可以一次刪除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

這是remove-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();
  }   
} 

當我檢查多個用戶時,第一個用戶將被刪除,而其他用戶則不會。 我的代碼中有任何錯誤嗎?

同樣,我要執行的操作是通過從表中選擇多個用戶並傳遞包含這樣的多個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代碼更改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;
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM