我无法删除Codeigniter中的记录

时间:2013-10-24 19:59:26

标签: php mysql codeigniter

我正在使用codeigniter学习CRUD。我有表名“发布”,coloumns就是这样(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)

请提供任何帮助或建议

3 个答案:

答案 0 :(得分:2)

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

    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-&gt; input-&gt;帖子更改为$ this-&gt; input-&gt; get < / p>

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; ?>

答案 1 :(得分:1)

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

答案 2 :(得分:0)

首先检查一下你的提交来自??输入类型 如果你不使用json

,应该提交(按钮)
  

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

你必须收到你要删除的变量

  

$ delete_id = $这 - &GT;输入 - &GT;柱( 'ID');

然后使用内部删除功能

  

$这 - &GT;负载&GT;模型( 'Post_Model');

     

$这 - &GT; Post_Model-&GT; delete_post( “ID”);

“发布”应该是表名,这应该是工作

相关问题