Delete Data using PHP & MySQL Database
Introduction
Deleting data from a MySQL database is a common operation in web development. In this guide, we will demonstrate how to perform this task using core PHP. The tutorial incorporates SEO best practices to ensure high visibility for related searches.
Prerequisites
- Basic understanding of PHP and MySQL.
- A MySQL database with a table containing data.
- A web server (e.g., XAMPP, WAMP, or a live server) to execute PHP scripts.
Steps to Delete Data in MySQL
1. Create a Database and Table
Run the following SQL commands to create a database and populate it with sample data:
CREATE DATABASE demo_db;
USE demo_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
age INT NOT NULL
);
INSERT INTO users (name, email, age) VALUES
('John Doe', 'john.doe@example.com', 25),
('Jane Smith', 'jane.smith@example.com', 30);
2. Establish a Database Connection in PHP
<?php
// Database connection settings
$host = 'localhost';
$user = 'root';
$password = ''; // Use your database password
$database = 'demo_db';
// Create connection
$conn = new mysqli($host, $user, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
3. Display Data with a Delete Option
<?php
include 'db_connection.php';
// Fetch all users from the database
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Delete User</title>
<meta name="description" content="Learn how to delete data using PHP and MySQL with this practical example.">
</head>
<body>
<h2>User List</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Action</th>
</tr>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['age']; ?></td>
<td>
<a href="delete.php?id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure you want to delete this record?');">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
4. Handle the Delete Request in PHP
Create a file named delete.php to process the delete action:
<?php
// Include the database connection file
include 'db_connection.php';
// Get the ID from the URL
$id = $_GET['id'];
// Prepare the SQL query to delete data
$sql = "DELETE FROM users WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
// Execute the query
if ($stmt->execute()) {
echo "Record deleted successfully.";
} else {
echo "Error deleting record: " . $conn->error;
}
// Close the connection
$stmt->close();
$conn->close();
// Redirect to the main page
header("Location: index.php");
exit();
?> Related Blogs
Sep 08, 2026
295 Views
Aug 31, 2026
415 Views
Jun 27, 2026
425 Views
Jun 24, 2026
340 Views
Jun 23, 2026
347 Views
Jun 10, 2026
324 Views
Jun 09, 2026
406 Views
Jun 08, 2026
318 Views
May 28, 2026
408 Views
May 14, 2026
487 Views
May 02, 2026
429 Views
Apr 26, 2026
581 Views
Apr 21, 2026
1046 Views
Apr 19, 2026
389 Views
Apr 15, 2026
395 Views
Mar 23, 2026
1623 Views
Mar 16, 2026
1934 Views
Mar 09, 2026
759 Views
Mar 02, 2026
581 Views
Feb 25, 2026
1062 Views
Feb 18, 2026
1725 Views
Feb 09, 2026
670 Views
Jan 31, 2026
868 Views
Jan 10, 2026
7042 Views
Dec 30, 2025
891 Views
Dec 20, 2025
870 Views
Dec 04, 2024
2497 Views
Nov 29, 2024
2058 Views
Nov 25, 2024
1807 Views
Nov 18, 2024
4004 Views
Nov 13, 2024
2662 Views
Nov 11, 2024
2102 Views
Nov 06, 2024
2588 Views
Nov 04, 2024
1939 Views
Oct 30, 2024
2612 Views
Oct 28, 2024
2562 Views
Oct 23, 2024
3586 Views
Oct 21, 2024
2107 Views
Oct 16, 2024
2901 Views
Oct 14, 2024
3490 Views
Oct 09, 2024
2010 Views
Oct 07, 2024
2196 Views
Oct 04, 2024
3116 Views
Sep 30, 2024
4896 Views
Sep 23, 2024
1958 Views
Sep 20, 2024
3313 Views
Aug 02, 2024
2196 Views
Jun 08, 2023
1742 Views
Jun 01, 2023
1872 Views
May 19, 2023
4072 Views
Jul 05, 2022
5049 Views
May 08, 2022
9402 Views
Apr 17, 2022
29552 Views
Dec 02, 2021
5579 Views
Sep 13, 2021
4950 Views
May 29, 2021
5540 Views
Apr 27, 2021
3781 Views
Apr 06, 2021
5812 Views
Apr 03, 2021
4360 Views
Mar 26, 2021
2883 Views
Mar 19, 2021
3774 Views
Mar 19, 2021
3147 Views
Mar 05, 2021
2983 Views
Feb 18, 2021
4150 Views
Feb 18, 2021
3484 Views
Feb 11, 2021
2977 Views
Nov 10, 2020
3263 Views
Sep 09, 2020
4609 Views
Aug 24, 2020
4950 Views
Aug 20, 2020
4589 Views
Aug 18, 2020
6240 Views
Jul 10, 2020
8399 Views
Jul 07, 2020
12076 Views
Jul 07, 2020
10328 Views
Jul 07, 2020
12927 Views
Jul 07, 2020
8693 Views
Jul 04, 2020
19819 Views
Jul 04, 2020
9858 Views
Jul 04, 2020
6906 Views
Jul 04, 2020
3604 Views
Jul 03, 2020
10068 Views
Jul 01, 2020
8283 Views
Jul 01, 2020
5839 Views
Jun 30, 2020
13883 Views
Jun 30, 2020
10574 Views
Jun 23, 2020
7784 Views
Jun 17, 2020
8124 Views
Jun 16, 2020
6074 Views
Jun 12, 2020
9439 Views
Jun 09, 2020
7340 Views


























.png)
_(1).jpg)
_(2).jpg)
.jpg)

_(1).jpg)

.jpg)
.jpg)
.jpg)
.jpg)
.jpg)


.jpg)
_(1).jpg)
.jpg)
.jpg)


_(1).png)

.png)

1.png)






























4.png)









0 Replies to “Delete Data Using PHP & MySql Database”
Leave a Reply
Your email address will not be published.