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

hfsqlsce  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(352)

我正试图使用php和ajax从表中删除一条记录,但当我单击该按钮时,显示执行该操作时出错。当我检查代码时,它会显示一个错误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;
}

有没有办法解决这个问题?

chy5wohz

chy5wohz1#

错误来自我的模型控制器我使用了错误的db key user\u id db中正确的key是当前的\u user\u 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;}
k10s72fa

k10s72fa2#

ci 3.1.19中存在的方法

$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-对于特定数据

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

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

相关问题