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.

display_view.php

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>Student Data</h2>
<table class="table table-striped" style="width:50%">
<thead>
<tr>
<th>Name</th>
<th>Roll Number</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php foreach($student as $row){?>
<tr>
<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
$row["id"];?>">Delete</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>

</body>
</html>

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

Crud_model.php

<?php
class Crud_model extends CI_Model
{
function insert_data($name,$roll_number,$class)
{
$query="insert into `student` values('','$name','$roll_number','$class')";
$this->db->query($query);
}

function display_data()
{
$query=$this->db->query("select * from `student`");
$res=$query->result_array();
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.

Crud_controller.php

<?php
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()
{
parent::__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
$this->load->view('insert_view');

// after submission code
if($this->input->post('insert'))
{

$name=$this->input->post('name');
$roll_number=$this->input->post('roll_number');
$class=$this->input->post('class');

$this->Crud_model->insert_data($name,$roll_number,$class);

echo "Data inserted Successfully";
}
}

// function to display data from database
public function display_fun()
{
$data['student']=$this->Crud_model->display_data();
$this->load->view('display_view',$data);
}

//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
redirect("http://localhost/ci/index.php/Crud_controller/display_fun");
}
}
?>

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

http://localhost/ci/index.php/Crud_controller/display_fun

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



Best WordPress Hosting


Share:


Leave a Reply


Comments
    Waiting for your comments