Delete data from MySQL table using CodeIgniter

  • Post author:
  • Post category:CodeIgniter
  • Post comments:0 Comments
  • Post last modified:October 5, 2020
  • Reading time:5 min(s) read

In this tutorial, you will get to learn how to Delete data from MySQL table using CodeIgniter. If you have not seen the previous tutorials of the Codeigniter framework, please check out the links are given below:

To insert data into the database, fetch data from the database & edit and update data from the database

So, now let’s see what is the procedure to delete a particular data from the database using CodeIgniter.

To edit a particular data we need to fetch their particular id from the database and send it through an anchor tag ( as we have done during edit and update) to a function where we want to send that particular id as shown in the below :

<!DOCTYPE html>
<html>
<head>
<title>Data fetching from database using codeigniter</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="card">

<div class="card-header">Data table <a style="float: right;" href="<?php echo base_url('New_con/form') ?>" class="btn btn-success">Add data</a> </div>

<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Created on</th>
<th>Action</th>


</tr>

</thead>
<tbody>
<?php foreach ($user_data as $key) {

?>
<tr>
<td><?php echo $key->name; ?></td> <!-- these are the static value "jj" how to show data dynamically ?? lets see -->
<td><?php echo $key->number; ?></td>
<td><?php echo $key->created; ?></td>
<td>
<a href="<?php echo base_url('New_con/edit_data') ?>/<?php echo $key->id ; ?>" class="btn btn-info btn-xs">Edit</a>

<a href="<?php echo base_url('New_con/delete_data') ?>/<?php echo $key->id ; ?>" onclick="return confirm('Are you sure!')" class="btn btn-danger btn-xs">Delete</a>
</td>
</tr>
<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

</div>

</body>
</html>

Then, We need to create a function/method in the controller as shown below:

public function delete_data($delete_id)
	{
		$data=$this->My_model->delete($delete_id);
		echo $data;
		$this->show_data();
	}

In the above code, We have set a parameter $delete_id to store the id of that particular data that has been sent through the anchor tag. Then we simply pass the $delete_id variable(id) to a function of our model. 

Thereafter we need to create a function in the model as shown in the below code:

public function delete($delete_id)
 	{
 $delete=$this->db->where('id',$delete_id)->delete('users');
 		if($delete){
 		return $msg="Data Deleted Successfully";
 		}
 	}
Delete data from MySQL table using CodeIgniter
Here is the complete code of Controller and model, Delete data from MySQL table using CodeIgniter as given below:

New_con.php(controller):

<?php
/**
* 
*/
class New_con extends CI_Controller
{

public function __construct()
{
parent:: __construct();

$this->load->helper('url');
$this->load->database();
$this->load->model('My_model');
}


public function index()
{
$this->load->view('a');
}


public function form()
{
$this->load->view('form');
}
public function data_insert()
{
$name=$this->input->post('name');
$phone=$this->input->post('phone');

$capsule = array('name' =>$name ,'number'=>$phone );

$msg=$this->My_model->save($capsule);

echo $msg;

$this->show_data();
}


public function show_data()
{

$data=$this->My_model->fetch();

$capsule = array('user_data' => $data );

$this->load->view('show',$capsule);


}

public function edit_data($edit_id)
{
$data=$this->My_model->edit($edit_id);

$capsule = array('e_data' => $data );

$this->load->view('edit',$capsule);

}

public function update_data()
{
$uid=$this->input->post('uid');
$name=$this->input->post('name');
$phone=$this->input->post('phone');

$capsule = array('name' =>$name ,'number'=>$phone );

$data= $this->My_model->update($capsule,$uid);

echo $data;

$this->show_data();
}

public function delete_data($delete_id)
{
$data=$this->My_model->delete($delete_id);

echo $data;
$this->show_data();
}
}


?>

My_model.php(Model):

<?php 

/**
* 
*/
class My_model extends CI_Model
{

public function save($capsule)
{
$insert=$this->db->insert('users',$capsule);
if ($insert) {
return $msg="Data Inserted Successfully";
}
}




public function fetch()
{
$query=$this->db->order_by('id',"DESC")->get('users')->result();

return $query;



}

public function edit($edit_id)
{ 
$query=$this->db->where('id',$edit_id)->get('users')->result();

return $query;
}

public function update($capsule,$uid)
{
$update=$this->db->where('id',$uid)->update('users',$capsule);
if ($update) {
return $msg="Data Updated Successfully";
}
}

public function delete($delete_id)
{
$delete=$this->db->where('id',$delete_id)->delete('users');
if($delete){
return $msg="Data Deleted Successfully";
}
}

}

?>

Please leave your suggestion on the comment box. to support us follow our social media links. Subscribe our youtube channel BeAppend. Follow on Instagram like your Facebook page.

Leave a Reply