Update delete data from database with codeigniter

Hello World, Welcome to the tutorial series Learn codeigniter from scratch. This is eight tutorial of this series. In this tutorial, we will learn how we can update delete data from database with codeigniter. We will implement update or delete action on our existing todo application. In the previous tutorial we have learned that how we can implement jquery ajax with codeigniter.

I have divided this tutorial in following parts:

  • Overview of concept
  • Update data in database with codeigniter
  • Delete data in database with codeigniter

Overview of concept

In this tutorial we will implement update and delete data from database with codeigniter. We have todo app and we will implement both action on this app. in the following screenshot you can see the edit and delete button for every task.


When user click on edit button then edit form will open and user can update the task. Similarly, When user click on delete button then confirmation box will open and when user confirm then task will be deleted from database.

Update data in database with codeigniter

In this step, we will update the data in database with edit form.

  • Create update.php file in application/views/todo folder with following code.
<!DOCTYPE html>
	<title><?php echo $page_title; ?></title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url('public/bootstrap.min.css') ?>" />
	<div class="container">
		<h1 class="page-header">Edit Todo List 
			<a href="<?php echo site_url('todo'); ?>" class="btn btn-sm btn-danger pull-right">Task List</a>
				echo $msg;
		<p><?php echo validation_errors(); ?></p>
		<form action="" method="post">
			<table class="table table-striped table-bordered">
					<th>Update Task</th>
					<td><input value="<?php echo $content['d']['task_title']; ?>" type="text" class="form-control" name="task_title" /></td>
					<td colspan="2">
						<input type="submit" class="btn btn-danger" value="Update Task" />
  • Open Todo.php controller and add the following method after ajax method.
// Update Data
	function update($id){
		$data['page_title']="Edit To-Do List";
		// Submit Form
		$this->form_validation->set_rules('task_title','Task Title','trim|required');
			// Process the data
  • Open Todo_Model.php file in models folder and add the following code after ajax_change_status method.
// Update Data
	function update($id){
			return 'Data has been updated';
			return 'Data has not been updated';

In the above code, you can see that we have added two methods. One is for getting the single task data and another is for update the data in database.

  • Open todo.php file in application/views/todo folder and change the link of edit and delete button like following code.
<a href="<?php echo site_url('todo/update/'.$task['id']); ?>" class="btn btn-xs btn-primary">Edit</a>
<a onclick="return confirm('Are you sure to delete data?')" href="<?php echo site_url('todo/delete/'.$task['id']); ?>" class="btn btn-xs btn-danger">Delete</a>

In the above code, i have added the JavaScript confirmation box so that user can double check the data before delete. We will work on delete action after update action.

  • Now, Run the application and try to edit the data. Edit form will show the data and you can easily update the task.

Delete data in database with codeigniter

In this step will delete data from database when user click on delete button and confirm. This is very easy step because we do not need to create any view.

  • Open Todo.php controller and add the following method after update method.
// Delete Data
	function delete($id){
  • Open Todo_Model.php in models folder and add the following method after update method.
// Delete Data
	function delete($id){

Now, When you click on delete button the confirmation box will appear as following screenshot and when you confirm then data will deleted.


Congratulations, 🙂 You have successfully perform the update delete action with codeigniter.

Leave a Reply

Your email address will not be published. Required fields are marked *