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
Jun 27, 2026
151 Views
Jun 24, 2026
145 Views
Jun 23, 2026
145 Views
Jun 10, 2026
161 Views
Jun 09, 2026
193 Views
Jun 08, 2026
120 Views
May 28, 2026
215 Views
May 14, 2026
244 Views
May 02, 2026
252 Views
Apr 26, 2026
311 Views
Apr 21, 2026
327 Views
Apr 19, 2026
206 Views
Apr 15, 2026
219 Views
Mar 23, 2026
1099 Views
Mar 16, 2026
1054 Views
Mar 09, 2026
513 Views
Mar 02, 2026
374 Views
Feb 25, 2026
796 Views
Feb 18, 2026
1297 Views
Feb 09, 2026
435 Views
Jan 31, 2026
445 Views
Jan 10, 2026
6139 Views
Dec 30, 2025
580 Views
Dec 20, 2025
647 Views
Dec 04, 2024
2312 Views
Nov 29, 2024
1892 Views
Nov 25, 2024
1616 Views
Nov 18, 2024
3487 Views
Nov 13, 2024
2315 Views
Nov 11, 2024
1946 Views
Nov 06, 2024
2316 Views
Nov 04, 2024
1746 Views
Oct 30, 2024
2357 Views
Oct 28, 2024
2367 Views
Oct 23, 2024
3340 Views
Oct 21, 2024
1905 Views
Oct 16, 2024
2642 Views
Oct 14, 2024
3231 Views
Oct 09, 2024
1828 Views
Oct 07, 2024
2019 Views
Oct 04, 2024
2776 Views
Sep 30, 2024
4214 Views
Sep 23, 2024
1778 Views
Sep 20, 2024
3078 Views
Aug 02, 2024
2034 Views
Jun 08, 2023
1553 Views
Jun 01, 2023
1720 Views
May 19, 2023
3557 Views
Jul 05, 2022
4880 Views
May 08, 2022
8804 Views
Apr 17, 2022
28888 Views
Dec 02, 2021
5388 Views
Sep 13, 2021
4676 Views
May 29, 2021
5377 Views
Apr 27, 2021
3621 Views
Apr 06, 2021
5436 Views
Apr 03, 2021
4127 Views
Mar 26, 2021
2739 Views
Mar 19, 2021
3549 Views
Mar 19, 2021
2833 Views
Mar 05, 2021
2838 Views
Feb 18, 2021
3963 Views
Feb 18, 2021
3298 Views
Feb 11, 2021
2843 Views
Nov 10, 2020
3096 Views
Sep 09, 2020
4419 Views
Aug 24, 2020
4773 Views
Aug 20, 2020
4396 Views
Aug 18, 2020
6008 Views
Jul 10, 2020
8234 Views
Jul 07, 2020
11892 Views
Jul 07, 2020
10098 Views
Jul 07, 2020
12726 Views
Jul 07, 2020
8442 Views
Jul 04, 2020
18995 Views
Jul 04, 2020
9708 Views
Jul 04, 2020
6753 Views
Jul 04, 2020
3450 Views
Jul 03, 2020
9895 Views
Jul 01, 2020
8101 Views
Jul 01, 2020
5718 Views
Jun 30, 2020
13697 Views
Jun 30, 2020
10414 Views
Jun 23, 2020
7663 Views
Jun 17, 2020
7959 Views
Jun 16, 2020
5876 Views
Jun 12, 2020
9273 Views
Jun 09, 2020
7101 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.