无法使用jquery和ajax删除多个用户(Codeingniter 3)

时间:2018-08-28 15:16:14

标签: php jquery mysql ajax codeigniter

我正在尝试使用 PHP AJAX 从表中按行ID删除记录,但是当我单击按钮时,它显示执行操作时出错。当我检查代码时,它显示错误500(内部服务器错误)。

这是我的JS函数:

function deleteSelUser()
{
    var data = new Array();
    $.each($("input[name='id[]']:checked"), function () {
        data.push($(this).val());
    });
    if (data == "")
    {
        swal("Error", "Please select at least one user.", "error");
        return false;
    }
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover this users!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: false
    },
            function () {

                //console.log(data);
                $.ajax({
                    url: SITE_URL + "depositoradmin/delete_select_user",
                    type: "POST",
                    data: {depositor_user_id: data},
                    success: function (res) {
                        // '.loader').hide();
                        var response = JSON.parse(res);
                        if (response.status == 1)
                        {
                            // swal("Success", response.msg, "success");
                            swal("Deleted!", response.msg, "success");
                            location.reload();
                        } else
                        {
                            swal("Error", response.msg, "error");
                        }
                    }
                });
            });
}

这是我的控制器功能:

function delete_select_user ()
{
    $depositor_user_id = implode("," , $this->input->post('depositor_user_id'));
    $data = array('is_deleted' => 1);
    $optresult = $this->depositor->delete_user_multiple($depositor_user_id , $data , 'depositor_user');
    if ($optresult) {
        echo json_encode(array('status' => 1 , 'msg' => "User deleted successfully"));
    } else {
        echo json_encode(array('status' => 0 , 'msg' => "User not deleted"));
    }
}

结束这是我的模型函数:

public function delete_user_multiple($depositor_user_id, $data, $table_name) {
    $this->db->where('user_id IN(' . $depositor_user_id . ')');
    $this->db->update($table_name, $data);
    return $depositor_user_id;
}

有关如何解决此问题的任何想法?

2 个答案:

答案 0 :(得分:0)

对于CI 3.1.19,存在where_in方法

$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')

https://www.codeigniter.com/userguide3/database/query_builder.html#looking-for-specific-data

$optresult = $this->depositor->delete_user_multiple($depositor_user_id , $data , 'depositor_user');
    if ($optresult) {
        echo json_encode(array('status' => 1 , 'msg' => "User deleted successfully"));
    } else {
        echo json_encode(array('status' => 0 , 'msg' => "User not deleted"));
    }

最好尝试捕获而不是测试结果(因为记录已被删除,查询可能返回0,这可能发生),并且您可以打印异常消息以查看出了什么问题

答案 1 :(得分:0)

错误来自我的模型控制器,我使用了错误的数据库密钥user_id,数据库中的正确密钥是current_user_id

public function delete_user_multiple($depositor_user_id, $data, $table_name) {
$this->db->where('user_id IN(' . $depositor_user_id . ')');
$this->db->update($table_name, $data);
return $depositor_user_id;}