douchi0471 2015-01-20 22:30
浏览 75
已采纳

使用PHP中的ajax调用删除多行

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.

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
                });                
            }
          });

    }

Example: The data in the url will be userID=1,2

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

When I check more than one user the first user will get delete, but not the others. Are there any mistakes in my code?

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.

  • 写回答

3条回答 默认 最新

  • dongxian8858 2015-01-23 20:44
    关注

    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.

    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;
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用