Through our previous post, you have learnt to insert data in database using CodeIgniter framework.

In this tutorial we explain you how to delete data from database using CodeIgniter framework.

Just Watch our live demo or download the delete_codeigniter.zip file from below link, extract files and include them in view, controller and model directory of your codeigniter framework as shown in the Read Me.txt file.

-: See Also :-

Insert Data using CodeIgniter 

Update Data in Database using CodeIgniter

VIEW FILE: delete_view.php

In this, we fetched all the names from data base and showed them in links. As user clicks on a particular name, its details appears on the right side with delete button.

user can click on Delete button to delete his/her record from table.

<!DOCTYPE html>
<html>
<head>
<title>Delete Data From Database Using CodeIgniter</title>
<!--=========== Importing Google fonts ===========-->
<link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'>
<link href="<?php echo base_url()?>./css/delete.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<div id="wrapper">
<h1>Delete Data From Database Using CodeIgniter</h1>
<div id="menu">
<p>Click On Menu</p>
<!--====== Displaying Fetched Names from Database in Links ========-->
<ol>
<?php foreach ($students as $student): ?>
<li><a href="<?php echo base_url() . "index.php/delete_ctrl/show_student_id/" . $student->student_id; ?>"><?php echo $student->student_name; ?></a>
</li><?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!--====== Displaying Fetched Details from Database ========-->
<?php foreach ($single_student as $student): ?>
<p>Student Detail</p>
<?php echo $student->student_name; ?>
<?php echo $student->student_email; ?>
<?php echo $student->student_mobile; ?>
<?php echo $student->student_address; ?>
<!--====== Delete Button ========-->
<a href="<?php echo base_url() . "index.php/delete_ctrl/delete_student_id/" . $student->student_id; ?>">
<button>Delete</button></a>
<?php endforeach; ?>
</div>
</div>
</div>
</body>
</html>

 

CONTROLLER FILE: delete_ctrl.php

copy the below file  in your controller directory.

<?php
class delete_ctrl extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('delete_model');
}
// Function to Fetch selected record from database.
function show_student_id() {
$id = $this->uri->segment(3);
$data['students'] = $this->delete_model->show_students();
$data['single_student'] = $this->delete_model->show_student_id($id);
$this->load->view('delete_view', $data);
}
// Function to Delete selected record from database.
function delete_student_id() {
$id = $this->uri->segment(3);
$this->delete_model->delete_student_id($id);
$this->show_student_id();
}
}
?>

 

MODEL FILE: delete_model.php

Create new class in your model as shown below.


<?php
class delete_model extends CI_Model{
// Function to select all from table name students.
function show_students(){
$query = $this->db->get('students');
$query_result = $query->result();
return $query_result;
}
// Function to select particular record from table name students.
function show_student_id($data){
$this->db->select('*');
$this->db->from('students');
$this->db->where('student_id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
// Function to Delete selected record from table name students.
function delete_student_id($id){
$this->db->where('student_id', $id);
$this->db->delete('students');
}
}
?> 

 

My SQL Code Segment:

To create database and table, execute following codes in your My SQL .

CREATE DATABASE company;
CREATE TABLE employee(
employee_id int(10) NOT NULL AUTO_INCREMENT,
employee_name varchar(255) NOT NULL,
employee_email varchar(255) NOT NULL,
employee_contact varchar(255) NOT NULL,
employee_address varchar(255) NOT NULL,
PRIMARY KEY (employee_id)
)

 

CSS FILE: delete.css

Styling HTML Elements.

#container{
width:960px;
height:610px;
margin:50px auto
}
#wrapper{
width:460px;
padding:0 50px 20px;
background:linear-gradient(#fff,#ABDBFF);
border:1px solid #ccc;
box-shadow:0 0 5px;
font-family:'Marcellus',serif;
float:left;
margin-top:10px
}
#menu{
float:left;
width:200px;
margin-top:-30px
}
#detail{
float:left;
width:220px;
padding:10px;
margin-top:-40px
}
a{
text-decoration:none;
color:blue
}
a:hover{
color:red
}
h1{
text-align:center;
font-size:28px
}
hr{
border:0;
border-bottom:1.5px solid #ccc;
margin-top:-10px;
margin-bottom:30px
}
button{
padding:6px 30px;
margin-top:10px;
font-size:14px;
background:linear-gradient(#fff,#828FEC);
border:2px solid #828FEC;
font-weight:700;
color:#8b008b
}
button:hover{
background:linear-gradient(#828FEC,#fff)
}
p{
font-size:18px;
font-weight:700;
color:#18af0b
}

Conclusion:

So, this was all about deleting data from database using codeigniter framework. keep following us to learn more.