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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?