delete operation


Deleting record-set is one of the easiest procedure. You simply need to retrieve the primary id of the record-set and execute the SQL delete statement.

Let's create a page for delete action first where we'll need the database connection as well.

BASIC PHP CODE TO DELETE DATA FROM DATABASE

<?php
// database connection
require_once('connect.php');

// check for primary id of the record-set in url
if(isset($_GET['id'])){

	// retrieve id from url
	$id = (int)$_GET['id'];

	// sql delete query
	$query = "DELETE FROM user_info WHERE id =" . $id;
}else{
	echo 'No id set';
}

//query execution
$result = mysqli_query($connect,$query);

//display message to user 
if($result){
	$_SESSION['success_message'] = 'User data deleted successfully';
	header('Location: insert.php');
}else{
	$_SESSION['error_message'] = 'User data couldn\'t be deleted';
	header('Location: insert.php');
}	

PASSING DATA ID TO THE DELETE.php FILE VIA GET METHOD (URL)

Quite simple right, but you need to pass the id from the display table via GET method for the query to get executed. So, we're going to add the query string to the delete icon using HTML anchor tag.

<a href="delete.php?id=<?=$data['id']?>"><i class="fa fa-trash"></i></a>	

This will add primary id of record-set in each row respectively. So, the record-set of the same row whose delete icon is clicked will get deleted.