CodeIgniter - Deleting Data from Database

CodeIgniter - Deleting Data from Database

0 1812

In this blog, we will retrieve delete the data from the database. In the given example, we will use the student table to elaborate on how to delete data from the database using the CodeIgniter framework.

Related Topics:

Displaying data from Database
Inserting data to Database
Codeigniter Interview Questions

How to delete Data:

Step 1 Open the application/views/display_view.php file and put the given code.


<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>

<div class="container">
<h2>Student Data</h2>
<table class="table table-striped" style="width:50%">
<th>Roll Number</th>
<?php foreach($student as $row){?>
<td><?php echo $row["name"];?></td>
<td><?php echo $row["roll_number"];?></td>
<td><?php echo $row["class"];?></td>
<td><a href="delete_fun?id=<?php echo
<?php } ?>


Step 2 Now open the application/models/Crud_model.php file and update the code.


class Crud_model extends CI_Model
function insert_data($name,$roll_number,$class)
$query="insert into `student` values('','$name','$roll_number','$class')";

function display_data()
$query=$this->db->query("select * from `student`");
return $res;

function delete_data($id)
$query=$this->db->query("delete from `student` where id='$id'");


Step 3 In the last, open the controller file application/controllers/Crud_controller.php and update the code.


defined('BASEPATH') OR exit('No direct script access allowed'); //for secuirty

class Crud_controller extends CI_Controller

//use constructor to connect with database and load model
public function __construct()
$this->load->database(); ///load database
$this->load->helper('url'); ///load URL helper to use redirect() method
$this->load->model('Crud_model'); ///load model

//function to insert data from database
public function insert_fun()

//load view

// after submission code



echo "Data inserted Successfully";

// function to display data from database
public function display_fun()

//function to delete record from table

public function delete_fun(){
$id=$this->input->get('id'); //receive id of the record
$this->Crud_model->delete_data($id); //load model

Step 4 Now, enter the given URL into the browser to delete the record.


Step 5 Click on the delete link to delete a specific record. In my case, I am deleting Ram.

Best WordPress Hosting


Leave a Reply

    Waiting for your comments