Delete data from datebase

In this tutorial, we are going to learn, how to create user delete with the help of click on delete button.

 

Step 1 : we will use the onClick function (delete_record) of JavaScript in the button and then we will pass the user ID to that onClick function.

<a href="javascript:;" onclick="delete_record(<?php echo $users->id; ?>)" class="btn btn-danger">Delete</a>

 

And then when we click on thet button, a confrimation popup will come up that we have to delete the user's data or not.

So for that we'll write JavaScript Code.

<script type="text/javascript">
  function delete_record(id) 
  {
    if(confirm('Do you want to delete?'))
    {
      document.location="dashboard/delete_user?uid="+id;
    }
  }
</script>

Where we have provided the document.location there we will provide a llick to delete the user detail so that the user clicks on it, so the user's data gets deleted. Now you can see that popup in the image.

 

 

Step 2 : Now open your dashboard controller and create delete_user() function. With the help of this function, we will pass the user's ID on the model so that we can delete the user's data and then we will show flash message on the response we get from there.

eg:

public function delete_user()
{
	$this->load->model('dashboard_model','userdata');
	$del_data = $this->userdata->delete_user_detail();
	if($del_data)
	{
		$this->session->set_flashdata('msg','User data has been deleted successfully!');
		$this->session->set_flashdata('msg_class','alert-success');	
	}
	else{
		$this->session->set_flashdata('msg','User data has not been deleted successfully!');
		$this->session->set_flashdata('msg_class','alert-danger');	
	}
	return redirect('dashboard');
}

Now you can see that flash message in the image.

 

Step 3 : Now now open your model file and create delete_user_detail() function. So that we can delete taht user's data from the Database with the help of that user ID.

public function delete_user_detail()
{
	$id = $_REQUEST['uid'];

	$this->db->where('id',$id);

	return $this->db->delete('userdata');
}

 


Result :

Source Code:

Codeigniter tutorial for beginners

In this project. We are providing you, how to create small project in Codeign....

Source Code