繁体   English   中英

我无法在Codeigniter中删除记录

[英]I can't delete record in Codeigniter

我正在Codeigniter中学习CRUD。 我的表名是“ posting”,列名是这样的(id,title,post)。 我成功创建了一个新帖子(都插入数据库并在视图中显示)。 但是我删除前端的帖子时遇到问题。 这是我的代码:

模型

Class Post_Model extends CI_Model{
  function index(){
     //Here is my homepage code
  }

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

控制者

Class Post extends CI_Controller{
  function delete()
  {
     $this->load->model('Post_Model');
     $this->Post_Model->delete_post("id");

     redirect('Post/index/', 'refresh');
  }
}

单击主页上的“删除”后,没有任何反应。 当我查看数据库时,我的记录仍然可用。

注意:(1)删除记录,我遵循codeigniter手册/用户指南,(2)按下前端的“删除”按钮后,发现消息错误(未定义变量:id)

有任何帮助或建议

您的问题是您没有发送对要删除的数据库中记录的引用

  Class Post extends CI_Controller{ function delete() { $this->load->model('Post_Model'); $this->Post_Model->delete_post("id"); redirect('Post/index/', 'refresh'); } } 

$this->Post_Model->delete_post("id"); 在这一行中,您需要发送对要删除的ID的引用,并发送字符串“ id”,因此在模型中其计算结果为:

DELETE * FROM `posting` WHERE id='id';

根据您的前端,您需要修改代码,以便它发送您要删除的帖子的ID。 我假设您是通过POST发送信息,因此可以执行此操作,如果您通过GET发送信息,则只需将$ this-> input-> post更改为$ this-> input-> get

Class Post extends CI_Controller{
  function delete()
  {
     $id_post = $this->input->post('your_post_name') //your_post_name is the name of the field sent in the form or whatever you're using to delete the post
     $this->load->model('Post_Model');
     $this->Post_Model->delete_post($id_post);

     redirect('Post/index/', 'refresh');
  }
}

希望能帮助到你。

//编辑看到您正在尝试从标签中删除,您可以执行以下操作

Class Post extends CI_Controller{
      function delete($id_post)
      {
         $this->load->model('Post_Model');
         $this->Post_Model->delete_post($id_post);

         redirect('Post/index/', 'refresh');
      }
    }

在您的视图中,在锚标记网址中发送帖子的ID

<?php foreach ($posts->result_array() as $mypost) : ?> 
    <tr> 
        <td width="62">Title</td> 
        <td width="15" align="center">:</td> 
        <td width="380"><?php echo $mypost['title']; ?></td> 
    </tr> 
    <tr> 
        <td width="62">Deskripsi</td> 
        <td width="15" align="center">:</td> 
        <td width="380"><?php echo $mypost['post']; ?></td> 
        <!-- Here you're sending the id in the database of the post you want to delete directly to your controller, once again Im assuming
            you're obtaining the ID along with the title and the post -->
        <td width="62" align="center"><a href="<?php echo base_url(); ?>post/delete/<?php echo $mypost['id'];?>">Delete</a></td>
    </tr> 
<?php endforeach; ?>
Class Post_Model extends CI_Model{
  function index(){
     //Here is my homepage code
  }

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

将post变量作为参数传递给delete_post()函数

Controller

Class Post extends CI_Controller{
  function delete()
  {
     $this->load->model('Post_Model');
     $this->Post_Model->delete_post($id);

     redirect('Post/index/', 'refresh');
  }
}

首先检查您的来源,您是提交来源吗? 如果您不使用json,则应提交输入类型(按钮)

表单动作应指向控制器和功能

您必须接收变量要删除的内容

$ delete_id = $ this-> input-> post('id');

然后使用内部删除功能

$ this-> load-> model('Post_Model');

$ this-> Post_Model-> delete_post(“ id”);

“发布”应为表名,这应该可行

暂无
暂无

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

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