Welcome Guest, Not a member yet? Register   Sign In
(ASk) I Can't "Delete" My Post
#1

[eluser]Unknown[/eluser]
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

Code:
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

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

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

View

This is my view code (edited). It works, except when I hit "Delete". I hope it can be clear question:

Code:
<?php foreach ($posts->result_array() as $mypost) : ?>
<tr>
    <td width="62">Title</td>
    <td width="15" align="center">:</td>
    <td width="380">&lt;?php echo $mypost['title']; ?&gt;</td>
</tr>
<tr>
    <td width="62">Deskripsi</td>
    <td width="15" align="center">:</td>
    <td width="380">&lt;?php echo $mypost['post']; ?&gt;</td>
    <td width="62" align="center"><a href="&lt;?php echo base_url(); ?&gt;post/delete">Delete</a></td>
    </tr>
&lt;?php endforeach; ?&gt;

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
#2

[eluser]Otemu[/eluser]
Hi,

Problem is your passing "id" as a string in your controller
Code:
$this->Post_Model->delete_post("id");
This needs to be a set variable, you could get the id from the url
Code:
&lt;?php echo base_url("post/delete".$mypost['post']); ?&gt;">

then set the id variable
Code:
$id = $this->uri->segment(3);

and then pass to your model
Code:
$this->Post_Model->delete_post($id);





Theme © iAndrew 2016 - Forum software by © MyBB