Search code examples
phpdatabasecodeigniterdelete-row

Deleting a specific row using codeigniter


Thank you for all the help and suggestions. Here's my answer to my problem:

view

<td><a href ="<?php echo site_url('helloworld/delete/'.$row->user_no);?>">delete</a></td>

controller

function delete($user_no) { 
    $this->load->model("dbmodel");
    $this->dbmodel->delete_row($user_no);

}

model

public function delete_row($id){
    $this -> db -> where('user_no', $id);
    $this -> db -> delete('users');
    redirect('helloworld/');
}

I hope this can help you :)


I am new in codeigniter. I'm trying to delete a specific row but I always get this error:

404 Page Not Found

The page you requested was not found.

Here is my code in my view:

<td><?php echo anchor('helloworld/delete_row?id='.$row->user_no, 'DELETE', 'id="$row->user_no"'); ?></td>

model:

function row_delete($id) {    
    $this->db->where('user_no', $id);   
    $this->db->delete('users');
}

controller:

function delete_row(){
    $id = $this->input->get('id');
    $this->load->model('dbmodel');
    $this->dbmodel->row_delete($id);
}

Solution

  • try to add directly an anchor

    <a href ="<?php echo site_url('controller/delete/'.$row->user_no);?>">delete</a>
    or
    <?php echo anchor('controller/delete/'.$row->user_no, 'Delete','title="delete"');?>
    

    model function :-

    public function deleteRecord($table, $where = array()) {
      $this->db->where($where);
      $res = $this->db->delete($table); 
      if($res)
        return TRUE;
      else
        return FALSE;
    }
    

    controller :-

    public function delete($id = '') {
      $this->load->model('dbmodel');
      $where = array('user_no' => $id); 
      $this->dbmodel->deleteRecord('table_name',$where);
    }