×

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

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...

6 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.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

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 Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

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.

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.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 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 Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 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.

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 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.