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 :-
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.
25 Replies to “CodeIgniter Delete Data From Database”
Hey, This articles helped me solve my delete structure, I’m sure I can solve my issue with my bootstrap modal inline table edit.
Thanks for this article kind sir.
Thanks for your appreciation !
Keep reading our blog posts.
nice one
wow fantastic it helps me a lot thanks alot …………
This article helped me complete my delete part.
Thanks a lot. Issue was resolved easily.
hay sir,
i am able to open insert from in my CI folder like this localhost/ci/index.php/insert_ctrl
but when i try to open Delete from it no gonna open like this localhost/ci/index.php/delete_ctrl
Where is the problem how i can open ?
i have the same problem. can i put all the controllers in one file? if that was the case, how can i invoke them?
how to delete multiple image in folder and database using codeigniter, i will appreciate that if anyone wil help me out, here is my script, delete from database success but folder still exist
http://pastebin.com/ZpSGFLJ6 view
http://pastebin.com/hdLcvqeH controller
http://pastebin.com/cqhhjzeX model
Thank you so much
I just want to know about the url that you made for view. Did you made any changes to file like for form open() type or left it same as it is.
What are you using url as??
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$student_id , stdClass::$student_Name
Filename: views/delete_view.php
Line Number: 18 its very humble request to admin or other guy pls..give solution
i tried again today & face the same problem
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$student_id
Filename: views/view_Delete.php
i change file name so don’t think it is mistake..
when i am runing delete program when show it “404 Page Not Found”
please properly pass id.
i tried running show_student_id instead of index of the controller as it is not there in the code above
Thanks Alot. its working perfectly keep it up (y)
hello asad ali can u pls tell me.. delete code .. becuz above delete functionality is not working i hv mention error when i m using this code
hey vivek,
This code is working perfectly i have used. Try this code again may be u will get success. 🙂
it really works
it was really helpful for freshers. please keep going on .
your database is not correct . u are using student database while u created company database
Hey!
I tested Your nice code (insert, update and delete) in CI 3.0.3 and the code really works nicely after little modification about: Database Table_Names, Model_names and Controller_names (this is because of the CI version).
I really have learned something … Thank You
employee table is not using,student table is using
The SQL doesn’t make sense. You’re creating a DB of employees and somehow trying to delete students? Please clarify.
This is My Model
———————
function delete_saveditem($id) {
$id = $_SESSION[‘user_id’];
$this->db->where(‘id = ‘,$id);
if ($this->db->delete(‘save3d’)) {
return true;
} else {
return false;
}
}
Controller
—————–
function deletesaveditems(){
$data[‘L_strErrorMessage’] = “”;
$data[‘err_msg’] = “”;
echo ”;print_r($_POST);
if($_POST[‘deletesavestyle’] != ” && count($_POST[‘deletesavestyle’] > 0)){
for($j =’0′; $juser_model->delete_saveditem($_POST[‘deletesavestyle’][$j]))
{
$this->session->set_flashdata(‘message’, ‘Saved Style deleted Successfully!’);
}
}
}
echo ‘hai’;
}
View
————-
<form class="form-horizontal" method="post" name="deletesavestyle" id="deletesavestyle" action="account/deletesaveditems/”>
………………………..
Delete
—————–
im getting o/p as
Array
(
[action] => deletesaveditems
)
hai
please help for gettiing correct output