HOW TO DO DELETE OPERATIONS IN PHP ?

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.

HOW TO DO DELETING OPERATIONS IN PHP ?

  • 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.

DELETE

FIRST STEP

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.

SECOND STEP

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.

 

<?php

include 'connect.php';

if (isset($_GET['delete'])) {
$datadelete = $db->prepare ("delete from personal where id=?");
$datadelete->execute([$_GET['delete']]);
header('Location:index.php');
}

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

?>

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

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

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

<tr>
<td>ID</td>
<td>FIRST NAME</td>
<td>SECOND NAME</td>
<td>E-MAIL</td>
<td>PROCESS</td>
</tr>

<?php

foreach ($takequery as $queries) {?>

<tr>
<td><?= $queries->id ?></td>
<td><?= $queries->first_name?></td>
<td><?= $queries->last_name?></td>
<td><?= $queries->email?></td>
<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>

</td>

</tr>

<?php } ?>

</table>
</div>
</div>

 

 

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

<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
-->
</body>
</html>

 

You did it!

This is screen!

HOW TO DO READING/LISTING OPERATIONS IN PHP ?

Hello everyone,

Today we will teach how to do crud operations in php. Above all, i would like to explain all our processing step by step.

HOW TO DO READING OPERATIONS IN PHP ?

  • Firstly, we have need to data. So, we will create sample data on mockaroo.
  • Then, we will create a new database on the Mysql.
  • Then, we will connect the database we have created with our PHP file.
  • Then, with the foreach loop, we will print the data from the database into the table we created.

READ

FIRST STEP

I created a table as personal data. This table have 4 columns. This columns ; id, first_name, last_name and email.

SECOND STEP

I will create a connect.php file for the connection to mysql database.

<?php
try {
$db = new PDO("mysql:host=localhost;dbname=personal", "root", "");
} catch ( PDOException $e ){
print $e->getMessage();
}
?>

THIRD STEP

I will create a index.php file for the list our mysql data. And i will include the con.php file I created insert this page. After created index.php before we will include con.php this page.

<?php

//We include our database file
include 'connect.php';

//We connect to the database and write our query
$query = $db->prepare("select * from personal");

$query->execute();
//We pass our data to array

$takequery = $query->fetchAll(PDO::FETCH_OBJ);

?>

FOURTH STEP

Then we will create a new bootstrap page and we will add bootstrap table. Shortly after We will list the data in the database in this table

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

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

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

<tr>
<td>ID</td>
<td>NAME</td>
<td>CITY</td>
<td>E-MAIL</td>
</tr>

<?php

//We loop the data we transferred to the array in the table

foreach ($takequery as $queries) {?>

<tr>
<td><?= $queries->id ?></td>
<td><?= $queries->name?></td>
<td><?= $queries->city?></td>
<td><?= $queries->email?></td>

</tr>

<?php } ?>

</table>
</div>
</div>

 

 

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

<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
-->
</body>
</html>

That’s all! 😀

You did it!

This is screen!