CodeIgniter Laravel PHP Example Javascript jQuery MORE
Need a Website Or Web Application Or Any Help In Code As Per Your Budget, Contact Us: +91 9437911966 (Whatsapp)

How to delete data from database using CodeIgniter framework

In this example we will discuss about how to delete a record or data from MySQL database using CodeIgniter framework PHP.

The DELETE statement is used to delete records from a table:

DELETE FROM table_name
WHERE some_column = some_value

Notice : The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

The students table look like this:

id first name last name Email Id Action
1 Divyasundar Sahu Delete
2 Hritika Sahu Delete
3 Milan Jena Delete

Now i am going to delete the id=3 record.

For delete record we use 3 file here

  1. Crud.php Path: codeIgniter\application\controllers\Crud.php
  2. Crud_model.php Path: codeIgniter\application\models\Crud_model.php
  3. display_records.php Path: codeIgniter\application\view\display_records.php

Crud Sql Table

  `id` int(11) NOT NULL,
  `first_name` varchar(30) NOT NULL,
  `last_name` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL,


class Crud extends CI_Controller 
	public function __construct()
	/*call CodeIgniter's default Constructor*/
	/*load database libray manually*/
	/*load Model*/
	public function displaydata()
	/*Delete Record*/
   public function deletedata()
	echo "Date deleted successfully !";


class Crud_model extends CI_Model 
	function display_records()
	$query=$this->db->query("select * from crud");
	return $query->result();
	function deleterecords($id)
	$this->db->query("delete  from crud where id='".$id."'");


<!DOCTYPE html>
<title>Delete records</title>
<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr style="background:#CCC">
    <th>Sr No</th>
    <th>Email Id</th>
  foreach($data as $row)
  echo "<tr>";
  echo "<td>".$i."</td>";
  echo "<td>".$row->first_name."</td>";
  echo "<td>".$row->last_name."</td>";
  echo "<td>".$row->email."</td>";
  echo "<td><a href='deletedata?id=".$row->id."'>Delete</a></td>";
  echo "</tr>";

Now run the program on your browser with the below URL:


After delete the record the table look like this.

id first name last name Email Id Action
1 Divyasundar Sahu Delete
2 Hritika Sahu Delete