×

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

Related Topics

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.