简体   繁体   中英

I can't delete record in Codeigniter

I'm learning CRUD in codeigniter. I have table name "posting" and the coloumns are like this (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

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:

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. 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

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

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

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

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

then use inside delete function

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

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

"posting" should be table name this should be work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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