TriMinh
TriMinh

Reputation: 59

Can not delete user in codeigniter

I can not delete user in codeigniter, When i click link to delete, it not working. It's a series on tutlus: Build a CMS in CodeIgniter.

link delete: /admin/user/delete/3

Controller:

class User extends Admin_Controller{

    public function __construct() {
        parent::__construct();
    } 

    public function delete() {
        $this->user_m->delete($id);
        redirect('admin/user');
    }
}

Model:

class User_M extends MY_Model {

 protected $_table_name = 'users';
 protected $_order_by = 'name';

     function __construct() {
    parent::__construct();
}
}

MY_Model:

class MY_Model extends CI_Model {

protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;

function __construct() {
    parent::__construct();
}
    public function delete($id){
        $filter = $this->_primary_filter;
        $id = $filter($id);

        if(!$id) {
        return FALSE;
        }
        $this->db->where($this->_primary_key, $id);
        $this->db->limit(1);
        $this->db->delete($this->_table_name);
    }

}

Upvotes: 0

Views: 189

Answers (3)

Nadeshwaran
Nadeshwaran

Reputation: 367

You have to get the id from the URL

   public function delete() {
   $this->load->helper('url');
   $id= $this->uri->segment(3) ;
            $this->user_m->delete($id);
            redirect('admin/user');
        }

Upvotes: 0

Dev
Dev

Reputation: 479

pass your id in User Controller

public function delete($id)
{
    $this->user_m->delete($id);
    redirect('admin/user');
}

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try to pass the $id that you got from URL into the function like

public function delete($id) {
    $this->user_m->delete($id);
    redirect('admin/user');
}

Upvotes: 2

Related Questions