简体   繁体   English

使用 codeigniter 无法取消链接和删除数据库上的行

[英]Trouble unlinking and deleting row on database using codeigniter

When I delete the first time, the unlink of the image works well and the row is deleted successfully from the data table.当我第一次删除时,图像的取消链接效果很好,并且从数据表中成功删除了该行。

The problem is that I'm using Ajax Jquery to request to delete and unlink the image so the page doesn't refresh and when I try to unlink and delete for the second time it doesn't have any response and the image doesn't unlink also the row isn't deleted.问题是我正在使用 Ajax Jquery 请求删除和取消链接图像,因此页面不会刷新,当我第二次尝试取消链接和删除时它没有任何响应并且图像没有取消链接也不会删除该行。

Also, I'm trying to use a jquery data table to load the rows but it doesn't sync the data.另外,我正在尝试使用 jquery 数据表来加载行,但它不会同步数据。

Ajax code: Ajax 代码:

var id = $('#deleteAdmin').data('id');

                $.ajax({
                    type : "POST",
                    url  : "<?php echo base_url(); ?>CRUD_Controller/crud_delete/" + id + "/administrador",
                    cache: false,
                    contentType: false, 
                    processData: false,
                    dataType: 'html',
                    success: function(data){


                        /*Func Ajax get admin without access*/
                        $.ajax({
                            type : "POST",
                            url  : "<?php echo base_url(); ?>CRUD_Controller/crud_getDataAll/administrador/administradores_null",
                            cache: false,
                            contentType: false, 
                            processData: false,
                            dataType: 'html',
                            success: function(data){

                                $('#example2 tbody').html(data);

                            },
                            error:function(xhr)
                            {
                                alert('Algo falhou, nao caregou a tabela. - ' + xhr.statusText);
                            }
                        });

                        $('#atribuir').modal('hide');

                        swalWithBootstrapButtons.fire(
                            'Removido!',
                            'O administrador foi removido do sistema com sucesso.',
                            'success'
                            );

                    },
                    error:function(xhr)
                    {
                        alert('Algo falhou, carregue a página novamente. - Erro : ' + xhr.statusText);
                    }
                });

Controller functions: Controller 功能:

public function crud_delete($id, $table_name)
    {
        switch ($table_name) {
            case 'administrador':
            $rows = $this->Administracao_model->getData($id, $table_name);
            foreach($rows as $row) {
                $imagem = $row->imagem;
                unlink(FCPATH.'assets/images/Administradores/'.$imagem);
            }
            $this->Administracao_model->deleteData($id, $table_name);
            break;
        }
    }

public function crud_getDataAll($table_name, $page_name)
    {
        switch ($page_name) {
            case 'administradores_null':
            $rows = $this->Administracao_model->getDataAdminSemAcesso($table_name);
            foreach($rows as $row) {
                echo '<tr>';
                echo '<td class="align-middle text-center"><a href="#">#ADMIN' . $row->id . '</a></td>';
                echo '<td class="align-middle text-center">' . $row->nome . '</td>';
                echo '<td class="align-middle text-center">' . $row->apelido . '</td>';
                echo '<td class="align-middle text-center">' . $row->cargo . '</td>';
                echo '<td class="align-middle text-center"><span class="badge badge-danger">Sem Acesso</span></td>';
                echo '<td class="align-middle text-center"><a href="#" class="btn btn-success text-white btn-sm" data-toggle="modal" data-id="'.$row->id.'" data-nome="'.$row->nome.'" data-apelido="'.$row->apelido.'" data-cargo="'.$row->cargo.'" data-contacto="'.$row->contacto.'" data-email="'.$row->email.'" data-imagem="'.$row->imagem.'" data-target="#atribuir" id="atrb"><i class="fas fa-edit"></i></a></td>';
                echo '</tr>';
            }
            break;

            default:
            echo "Erro no contnroller.";
            break;
        }
    }

Models:楷模:

function getDataAdminSemAcesso($table_name)
    {
        $this->db->select('*');
        $this->db->where('id_nivel', null);
        $query = $this->db->get($table_name);
        return $query->result();
    }

function getData($id, $table_name)
    {
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get($table_name);
        return $query->result();
    }

function deleteData($id, $table_name)
    { 
        $this->db->where('id', $id);
        $this->db->delete($table_name);
    }

Big thanks in advance!提前非常感谢!

Usually if you use an id or class when performing a delete process, you must call the function again to process the delete.通常,如果在执行删除过程时使用 id 或 class,则必须再次调用 function 来处理删除。

My suggestion is that you only delete rows without having to update the data in the table.我的建议是你只删除行而不必更新表中的数据。

You can add id to tr, and deleted when the process was successful可以给tr添加id,成功后删除

var id = $('#deleteAdmin').data('id');

$.ajax({
    type : "POST",
    url  : "<?=base_url()?>CRUD_Controller/crud_delete/" + id + "/administrador",
    cache: false,
    contentType: false, 
    processData: false,
    dataType: 'html',
    success: function(data){
        $("#"+ id).remove(); // Delete row
    },
    error:function(xhr)
    {
        alert('Algo falhou, carregue a página novamente. - Erro : ' + xhr.statusText);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM