Update delete data from database with codeigniter

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.

todo-list-edit-delete-button

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>
<html>
<head>
	<title><?php echo $page_title; ?></title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url('public/bootstrap.min.css') ?>" />
</head>
<body>
	<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>
		</h1>
		<p>
			<?php
			if(isset($msg)){
				echo $msg;
			}
			?>
		</p>
		<p><?php echo validation_errors(); ?></p>
		<form action="" method="post">
			<table class="table table-striped table-bordered">
				<tr>
					<th>Update Task</th>
					<td><input value="<?php echo $content['d']['task_title']; ?>" type="text" class="form-control" name="task_title" /></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" class="btn btn-danger" value="Update Task" />
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
  • 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');
		if($this->form_validation->run()===true){
			// Process the data
			$dbRes=$this->Todo_model->update($id);
			$data['msg']=$dbRes;
		}
		$data['content']=$this->Todo_model->get_single_list($id);
		$this->load->view('todo/update.php',$data);
	}
  • Open Todo_Model.php file in models folder and add the following code after ajax_change_status method.
// Update Data
	function update($id){
		$updateData=array(
			'task_title'=>$this->input->post('task_title'),
			'task_status'=>'pending'
		);
		$this->db->where('id',$id);
		$updateQuery=$this->db->update('todo_list',$updateData);
		if($this->db->affected_rows()>0){
			return 'Data has been updated';
		}else{
			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){
		$dbRes=$this->Todo_model->delete($id);
		redirect('todo');
	}
  • Open Todo_Model.php in models folder and add the following method after update method.
// Delete Data
	function delete($id){
		$res=array();
		$this->db->where('id',$id);
		$this->db->delete('todo_list');
	}

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

confirm-box

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

Leave a Reply