首页 > 解决方案 > 删除项目代码点火器

问题描述

我很难从 CRUD 中删除记录

这是我的控制器。我在哪里有我的包含、编辑、列出和删除功能

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Contatocliente extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('contato_model');
    }

    public function index()
    {
        $data['contato']=$this->contato_model->get_all_contato();
        $this->load->view('admin/contatocliente/contatocliente',$data);
    }

    public function contato_add()
    {
        $data = array(
            'contato_id' => $this->input->post('contato_id'),
            'contato_nome' => $this->input->post('contato_nome'),
            'contato_email' => $this->input->post('contato_email'),
            'contato_mensagem' => $this->input->post('contato_mensagem'),
            'contato_cadastro' => $this->input->post('contato_cadastro'),
        );
        $insert = $this->contato_model->contato_add($data);
        echo json_encode(array("status" => TRUE));
    }

    public function ajax_edit($id)
    {
        $data = $this->contato_model->get_by_id($id);

        echo json_encode($data);
    }

    public function contato_update()
    {
        $data = array(
            'contato_id' => $this->input->post('contato_id'),
            'contato_nome' => $this->input->post('contato_nome'),
            'contato_email' => $this->input->post('contato_email'),
            'contato_mensagem' => $this->input->post('contato_mensagem'),
            'contato_cadastro' => $this->input->post('contato_cadastro'),
        );
        $this->contato_model->contato_update(array('contato_id' => $this->post('contato_id')), $data);
        echo json_encode(array("status" => TRUE));
    }

    public function contato_delete($id)
    {
        $this->contato_model->delete_by_id($id);
        echo json_encode(array("status" => TRUE));
    }
}

这是我的模特

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Contato_model extends CI_Model
{

    var $table = 'contato';


    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    //LISTA TODOS OS REGISTROS
    public function get_all_contato()
    {
    $this->db->from('contato');
    $query=$this->db->get();
    return $query->result();
    }

    // BUSCAR UM ÚNICO REGISTRO
    public function get_by_id($id)
    {
        $this->db->from($this->table);
        $this->db->where('contato_id',$id);
        $query = $this->db->get();

        return $query->row();
    }

    // ADICIONA UM REGISTRO
    public function contato_add($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    // ATUALIZA UM REGISTRO
    public function contato_update($where, $data)
    {
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }

    // DELETA UM REGISTRO
    public function delete_by_id($id)
    {
        $this->db->where('contato_id', $id);
        $this->db->delete($this->table);
    }
}

而我的看法是这样的

<?php foreach($contato as $contatos){?>
<tr>
    <td><?php echo $contatos->contato_id;?></td>
    <td><?php echo $contatos->contato_nome;?></td>
    <td><?php echo $contatos->contato_contato;?></td>
    <td><?php echo $contatos->contato_email;?></td>
    <td><?php echo $contatos->contato_cadastro;?></td>
    <td><?php echo $contatos->contato_mensagem;?></td>
    <td>
        <button class="btn btn-warning" onclick="edit_contato(<?php echo $contatos->contato_id;?>)"><i class="glyphicon glyphicon-pencil"></i></button>
        <button class="btn btn-danger" onclick="delete_contato(<?php echo $contatos->contato_id;?>)"><i class="glyphicon glyphicon-remove"></i></button>
    </td>
</tr>

还有我的 Javascript

  function delete_contato(id)
{
if(confirm('Are you sure delete this data?'))
{
    // ajax delete data from database
    $.ajax({
        url : "<?php echo site_url('admin/contatocliente/contato_delete')?>/"+id,
        type: "POST",
        dataType: "JSON",
        success: function(data)
        {
        location.reload();
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error deleting data');
        }
    });
}
}

当我单击删除按钮时,会出现以下消息:

“删除数据时出错”

标签: javascriptphphtmlcodeigniter

解决方案


public function contato_delete()
{
    $id    = $this->input->post('id');
    $this->contato_model->delete_by_id($id);
    echo json_encode(array("status" => TRUE));
}

public function delete_by_id($id)
{
    $this->db->where('contato_id', $id);
    $this->db->delete($this->table);
}



$.ajax({
        url : "<?php echo site_url('admin/contatocliente/contato_delete')?>",
        data : {
         id : id
        }
        type: "POST",
        dataType: "JSON",
        success: function(data)
        {
        location.reload();
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error deleting data');
        }
    });




<button class="btn btn-warning" onclick="edit_contato('<?php echo $contatos->contato_id;?>')"><i class="glyphicon glyphicon-pencil"></i></button>
<button class="btn btn-danger" onclick="delete_contato('<?php echo $contatos->contato_id;?>')"><i class="glyphicon glyphicon-remove"></i></button>

试试这个


推荐阅读