【问题标题】:delete data from table using CI使用 CI 从表中删除数据
【发布时间】:2020-12-28 18:56:57
【问题描述】:

我尝试从数据库中获取数据并将其放入表中,我还使用实时搜索来过滤数据。现在我想尝试用按钮删除一些行,我在每一行都制作按钮,所以当我按下删除按钮时,1 行从我的表和我的数据库中消失了。

但是控制台说没有用

[HTTP/1.1 500 内部服务器错误 70ms]

我不知道出了什么问题,请帮助我,非常感谢。 这是我的代码,也许可以帮助您找到我的错误

这是我的代码。

这里是控制器

function fetchData(){
        $output = '';
        $query = '';
        $this->load->model('pool_method');
        if($this->input->post('query'))
        {
            $query = $this->input->post('query');
        }
        $data = $this->pool_method->searchData($query);
        $json = array();
        $output .= '
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
            <tr>
            <th>Nama Barang</th>
            <th>Keterangan</th>
            <th>Tanggal Pembelian</th>
            <th>QTY</th>
            <th>Harga</th>
            <th>Jumlah</th>
            </tr>
        ';
        if($data->num_rows() > 0)
        {
            foreach($data->result() as $row)
            {               
                $output .= '
                <tr>
                <td>'.$row->nama_barang.'</td>
                <td>'.$row->keterangan.'</td>
                <td>'.$row->tanggal_pembelian.'</td>
                <td>'.$row->qty.'</td>
                <td>'.$row->harga.'</td>
                <td>'.$row->jumlah.'</td>
                <td>'.'<button type="button" name="delete" id="'.$row->id_pembelian_pool.'" class="btn btn-danger btn-xs delete">Delete</button>'.'</td>
                </tr>
                ';
                
            }
        }
        else
        {
            $output .= '<tr>
                <td colspan="5">No Data Found</td>
                </tr>';
        }
            $output .= '</table>';
            echo $output;
    }
    function deleteData()
    {
             $this->pool_method->delete_singel_row($_POST["id_pembelian_pool"]);
        echo 'Data Deleted';
    }

型号

function delete_single_row($id_pembelian_pool)
    {
        $this->db->where('id_pembelian_pool',$id_pembelian_pool);
        $this->db->delete('pembelian_pool');
    }

JS

<script>
    $(document).on('click', '.delete', function(){  
       var id_pembelian_pool = $(this).attr("id_pembelian_pool");  
       if(confirm("Are you sure you want to delete this?"))  
       {  
            $.ajax({  
                 url:"<?php echo base_url(); ?>Manual_co/deleteData",  
                 method:"POST",  
                 data:{id_pembelian_pool:id_pembelian_pool},  
                 success:function(data)  
                 {  
                      alert(data);  
                      dataTable.ajax.reload();  
                 }  
            });  
       }  
       else  
       {  
            return false;       
       }  
  });  
</script>

感谢您的帮助,您的帮助对我来说意义重大

【问题讨论】:

    标签: javascript php html ajax codeigniter


    【解决方案1】:

    我认为您在要从中获取密钥的属性名称上犯了一个小错误。该属性称为id,因此更改此行

    var id_pembelian_pool = $(this).attr("id_pembelian_pool");  
    

    var id_pembelian_pool = $(this).attr("id");  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多