CodeIgniter Delete Data

In this section, We'll understand how to delete data from database.

Using a text editor, copy the below code in your controller called User.php. In it, place this code and save it to your application/controllers/ directory:

<?php
  
   class User extends CI_Controller {
   
      public function __construct() { 
         parent::__construct(); 
         $this->load->helper(array('form', 'url')); 
      } 
	
      public function index() {
			
         /* Load form validation library */ 
         $this->load->library('form_validation');
			
	 /* Validation rule */
	 $this->form_validation->set_rules('name', 'Name', 'required');
	 $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
	 $this->form_validation->set_rules('contact_no', 'Contact Number', 'required');		 
			
         if ($this->form_validation->run() == FALSE) { 
            $this->load->view('user_form'); 
         } 
         else { 
                    $this->load->model('user_model');
		    $this->user_model->save();
		    $success = "Submit successfully!";
                    $this->load->view('user_form', compact('success')); 
         } 
      }
	  
     public function edit($id) {
			
         /* Load form validation library */ 
         $this->load->library('form_validation');
			
	 /* Validation rule */
	 $this->form_validation->set_rules('name', 'Name', 'required');
	 $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
	 $this->form_validation->set_rules('contact_no', 'Contact Number', 'required');		 
			
         if ($this->form_validation->run() == FALSE) { 
            $this->load->view('user_edit'); 
         } 
         else { 
                    $this->load->model('user_model');
		    $this->user_model->update($id);
		 }
	 }
	  
	  public function getdata()
	     {
		   $this->load->model('user_model');
		   $data['record'] = $this->user_model->getdata();
		   $this->load->view('display_users', $data);
	     }
		 
	   public function getrecordbyid($id)
	     {
		   $this->load->model('user_model');
		   $data['record'] = $this->user_model->getuserinfobyid($id);
		   $this->load->view('user_edit', $data);
	     }
		 
	  public function deleteuserbyid($id)
	     {
		   $this->load->model('user_model');
		   $this->user_model->deleteuserbyid($id);
	     }
  }
?>

Using a text editor, copy the below code in your model called User_model.php. In it, place this code and save it to your application/models/ directory:

<?php
class User_model extends CI_Model
{
    public function save()
    {
	   $data['name'] = $this->input->post('name');
	   $data['email'] = $this->input->post('email');
	   $data['contact_no'] = $this->input->post('contact_no');
	   $this->db->insert('tbl_users', $data);
    }
	   
	   public function getdata()
	     {
		    return $this->db->get('tbl_users')->result();	
	     }
		 
	   public function getuserinfobyid($id)
	     {
		    return $this->db->where('id', $id)->get('tbl_users')->row();
	     }
		 
    
        public function update($id)
	    {
		   $data['name'] = $this->input->post('name');
		   $data['email'] = $this->input->post('email');
		   $data['contact_no'] = $this->input->post('contact_no');
		   $this->db->where('id', $id)->update('tbl_users', $data);
		   redirect('user/getdata');
	    }
		 
	   public function deleteuserbyid($id)
	     {
		   $this->db->where('id', $id)->delete('tbl_users');
		   redirect('user/getdata');
	     }
		 
  }

To try to delete the record, click on delete link displayed in listing page