How To Do Delete Operations In PHP ?

By admin
Nisan 28, 2022
4 min read

Hello everyone,

Today we will learn deleting operations in php. We saw data listing in the previous lesson. Above all, i would like to explain all our processing step by step.


  • We can do the deletion with php’s GET function. If we can capture the IDs of the data, we can do the deletion with a single line of code.
  • For this, we will create a field called PROCESS in the table and put the DELETE button.
  • And then we will create get function links and write its delete query.



We created a new column by adding td in the table field and added one of the bootstrap buttons to this column. We tried to get the id of each clicked data using the get function.

<td><a href="?delete=<?= $queries->id ?> <button type="button" class="btn btn-outline-danger">DELETE</button></a></td>

We created a column named PROCESS where the table names are. In order to bring the DELETE button to each row that will come under this column, we added a new td to the secondary td field we added and put a bootstrap button in this column. Thus, each time the DELETE button is clicked, we will be able to easily retrieve the identity of each data that enters the loop.


After detecting the clicked id, we need to prepare the query and code for the delete operation. First of all, we prepare the query and we need to perform the deletion process according to the id we have captured just below.



include 'connect.php';

if (isset($_GET['delete'])) {
$datadelete = $db->prepare ("delete from personal where id=?");

include 'connect.php';
$query = $db->prepare("select * from personal");
$takequery = $query->fetchAll(PDO::FETCH_OBJ);


<!doctype html>
<html lang="en">
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Bootstrap CSS -->
<link href="" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<title>LIST SCREEN</title>
<h1 class="display-1 text-center">USER LIST</h1>
<div class="container">
<div class="row">
<table class="table table-striped table-hover mt-3">

<td>FIRST NAME</td>
<td>SECOND NAME</td>


foreach ($takequery as $queries) {?>

<td><?= $queries->id ?></td>
<td><?= $queries->first_name?></td>
<td><?= $queries->last_name?></td>
<td><?= $queries->email?></td>
<a href="?delete=<?= $queries->id ?>" onclick="return confirm('Are you sure you want to delete data?')" ><button type="button" class="btn btn-outline-danger">DELETE</button></a>



<?php } ?>




<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<script src="" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>


You did it!

This is screen!

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir