Delete a record from Database CodeIgniter

Delete a record from the Database

After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table.

To delete a record, first, we have to fetch all the records from a database and show them with the delete link. For this, a delete() function is used to delete a selected record, as shown below:

Syntax

delete([$table = ‘’[, $where = ‘’[, $limit = NULL [, $reset_data = TRUE ]]]])

 It has four parameters:

$table: It defines the table name from that a particular record to be deleted

$where: It defines the filed id to be deleted.

$limit: It is used to set the limit in a table.

$reset_data: It determines whether to reset the query.

Use the following method to delete a row from the table employees. The first parameter represents the name of the table from which data will be deleted, and the second parameter tells which record to be deleted.

$this->db->delete(“employees”, “id = 5”);

To delete a record in a database table, we need to create the following files, as shown below:

  1. Employee_controller.php
  2. Data_Model.php 
  3. fetch.php

Create a controller file Employee_controller.php and save it in application/controller/Employee_controller.php. And write the following program in the controller file.

Employee_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Employee_controller extends CI_Controller 
 {
  function _construct()
     {
         parent::_construct();
         $this->load->model('Data_Model'); 
         $this->load->library('uri');
     } 
 public function index()
 { 
     $this->load->database();
     $this->load->model('Data_Model');
     $records['read_data'] = $this->Data_Model->read_data();
   $this->load->view('fetch',$records);  } 
 public function del_data()
 {
     $id = $this->uri->segment(3);
     $this->load->model('Data_Model');
     if($this->Data_Model->del_data($id))
     { 
         echo "error"; 
     }
     else
 { 
         redirect('/Employee_controller/deleted');
     }
 }
 public function deleted()
 { 
   $this->load->library('session'); 
      $this->session->set_flashdata('data',' Successfully Deleted');
     $this->index();
 }
 }
 ?> 

Create a model file Data_Model.php and save it in application/model/Data_Model.php. After that, write the following program in the model file.

Data_Model.php

<?php
  class Data_Model extends CI_Model
 {
 public function __construct() {
 parent::__construct();
 $this->load->database();
 } 
 public function read_data()
 {
     $this->load->database();
 $this->db->select('*');
 $this->db->order_by('id', 'asc');
 $this->db->from("employees");
 $query = $this->db->get();
 return $query->result_array(); 
 public function read_data()
 {
     $this->load->database();
 $this->db->select('*');
 $this->db->order_by('id', 'asc');
 $this->db->from("employees");
 $query = $this->db->get();
 return $query->result_array(); 
 public function read_data()
 {
     $this->load->database();
 $this->db->select('*');
 $this->db->order_by('id', 'asc');
 $this->db->from("employees");
 $query = $this->db->get();
 return $query->result_array(); 
 public function read_data()
 {
     $this->load->database();
 $this->db->select('*');
 $this->db->order_by('id', 'asc');
 $this->db->from("employees");
 $query = $this->db->get();
 return $query->result_array(); 
 }
 public function del_data($id)
 {
     $this->db->where("id", $id);
     $this->db->delete("employees");
 }
 }
 ?> 

Create a view file fetch.php and save it in application/views/fetch.php. After that, write the following program in the view file.

fetch.php

<!Doctype>
 <html>
 <head>
 <title> Tutorial and Example </title>
 </head>
 <body>
 <h2> Employee Details </h2>
 <div id="container"> 
 <p> <b><?php echo anchor('Employee_controller/insert_data', 'Add Employee'); ?> </b></p>
 <CENTER><h2 style="color:green;"><?php echo $this->session->flashdata('data'); ?></h2></CENTER><br>
 <h2></h2> 
 <table border = 1>
     <tr>
         <th cellpadding = "20"> Id </th> 
         <th cellspacing = "20"> Name </th>
         <th cellpadding = "20"> Email </th>
         <th cellpadding = "20"> Employee Age</th>
             <th cellpadding = "20"> Company </th>
         <th cellspacing = "20"> Mobile No.  </th>
         <th cellspacing = "20"> Country</th> 
         <th> Update</th>
         <th> Delete </th> 
             </tr>
     <?php foreach($read_data as $row): ?>
         <tr>
         <td> <?= $row['id']; ?> </td>
                         <td> <?= $row['name']; ?> </td>
                         <td> <?= $row['email']; ?> </td>
         <td> <?= $row['age']; ?> </td>
                         <td> <?= $row['company']; ?> </td>
         <td> <?= $row['mob']; ?> </td>
         <td> <?= $row['country']; ?> </td> 
         <td> <?php echo anchor("Employee_controller/update_id/$row[id]", "Edit"); ?> </td>
         <td> <?php echo anchor("Employee_controller/del_data/$row[id]", "Delete"); ?> </td> 
         </tr>
         <?php endforeach; ?>
 </table>
 </div>
 </body>
 </html> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Employee_controller function; it shows the output as shown below.

Suppose in this example; we are going to delete a record no. 5. As we click on the Delete link of the id no.5, it removes the particular record from the list, and it displays the following message, as shown in the below image. 

Delete a record from Database

Furthermore, if we want to check that the record has been successfully deleted from the database, go to phpMyAdmin, and see the table records, as shown in the below image.

Delete a record from Database



ADVERTISEMENT
ADVERTISEMENT