简体   繁体   English

我无法在Codeigniter中删除记录

[英]I can't delete record in Codeigniter

I'm learning CRUD in codeigniter. 我正在Codeigniter中学习CRUD。 I have table name "posting" and the coloumns are like this (id, title, post). 我的表名是“ posting”,列名是这样的(id,title,post)。 I successed to create a new post (both insert into database and display in the view). 我成功创建了一个新帖子(都插入数据库并在视图中显示)。 But I have problem when I delete my post in the front-end. 但是我删除前端的帖子时遇到问题。 Here is my code: 这是我的代码:

Model 模型

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');
  }
}

Controller 控制者

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

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

After click "delete" in the homepage, there was nothing happens. 单击主页上的“删除”后,没有任何反应。 While I'm looking into my database, my records still available. 当我查看数据库时,我的记录仍然可用。

Note: (1) to delete record, I'm following the codeigniter manual / user guide, (2) I found a message error (Undefined variable: id) after hiting the "delete" button in the front-end 注意:(1)删除记录,我遵循codeigniter手册/用户指南,(2)按下前端的“删除”按钮后,发现消息错误(未定义变量:id)

Any help or suggestion, please 有任何帮助或建议

Your problem is that you're not sending reference to the record in the database you want to delete 您的问题是您没有发送对要删除的数据库中记录的引用

  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"); In this line you need to send reference to the ID you want to delete, you're sending the string "id", so in your model it evaluates to: 在这一行中,您需要发送对要删除的ID的引用,并发送字符串“ id”,因此在模型中其计算结果为:

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

Depending on your front end you need to modify your code so it sends the id of the post you want to delete. 根据您的前端,您需要修改代码,以便它发送您要删除的帖子的ID。 Im assuming you're sending the information via POST so you could do this, if you're sending it via GET you only change the $this->input->post to $this->input->get 我假设您是通过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');
  }
}

Hope it helps. 希望能帮助到你。

//Edit seeing you're trying to delete from an tag you can do the following //编辑看到您正在尝试从标签中删除,您可以执行以下操作

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');
      }
    }

In your view send the id of the post along in the anchor tag url 在您的视图中,在锚标记网址中发送帖子的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');
  }
}

pass post variable as parameter in delete_post() function 将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');
  }
}

first check out your from are you submitting the from?? 首先检查您的来源,您是提交来源吗? input type should be submit (button) if you are not using json 如果您不使用json,则应提交输入类型(按钮)

and form action should point to the controller and function 表单动作应指向控制器和功能

you have to receive variable what you going to delete 您必须接收变量要删除的内容

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

then use inside delete function 然后使用内部删除功能

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

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

"posting" should be table name this should be work “发布”应为表名,这应该可行

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

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