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
89 Views
Jun 24, 2026
97 Views
Jun 23, 2026
102 Views
Jun 10, 2026
131 Views
Jun 09, 2026
127 Views
Jun 08, 2026
87 Views
May 28, 2026
170 Views
May 14, 2026
197 Views
May 02, 2026
225 Views
Apr 26, 2026
269 Views
Apr 21, 2026
233 Views
Apr 19, 2026
181 Views
Apr 15, 2026
182 Views
Mar 23, 2026
1024 Views
Mar 16, 2026
850 Views
Mar 09, 2026
478 Views
Mar 02, 2026
333 Views
Feb 25, 2026
729 Views
Feb 18, 2026
1254 Views
Feb 09, 2026
399 Views
Jan 31, 2026
392 Views
Jan 10, 2026
5827 Views
Dec 30, 2025
538 Views
Dec 20, 2025
596 Views
Dec 04, 2024
2280 Views
Nov 29, 2024
1871 Views
Nov 25, 2024
1591 Views
Nov 18, 2024
3383 Views
Nov 13, 2024
2254 Views
Nov 11, 2024
1922 Views
Nov 06, 2024
2274 Views
Nov 04, 2024
1711 Views
Oct 30, 2024
2325 Views
Oct 28, 2024
2302 Views
Oct 23, 2024
3288 Views
Oct 21, 2024
1867 Views
Oct 16, 2024
2609 Views
Oct 14, 2024
3160 Views
Oct 09, 2024
1789 Views
Oct 07, 2024
1985 Views
Oct 04, 2024
2715 Views
Sep 30, 2024
4116 Views
Sep 23, 2024
1747 Views
Sep 20, 2024
3026 Views
Aug 02, 2024
2011 Views
Jun 08, 2023
1526 Views
Jun 01, 2023
1699 Views
May 19, 2023
3450 Views
Jul 05, 2022
4854 Views
May 08, 2022
8668 Views
Apr 17, 2022
28735 Views
Dec 02, 2021
5353 Views
Sep 13, 2021
4633 Views
May 29, 2021
5336 Views
Apr 27, 2021
3598 Views
Apr 06, 2021
5332 Views
Apr 03, 2021
4089 Views
Mar 26, 2021
2717 Views
Mar 19, 2021
3506 Views
Mar 19, 2021
2794 Views
Mar 05, 2021
2814 Views
Feb 18, 2021
3907 Views
Feb 18, 2021
3271 Views
Feb 11, 2021
2817 Views
Nov 10, 2020
3079 Views
Sep 09, 2020
4382 Views
Aug 24, 2020
4741 Views
Aug 20, 2020
4359 Views
Aug 18, 2020
5966 Views
Jul 10, 2020
8205 Views
Jul 07, 2020
11856 Views
Jul 07, 2020
10071 Views
Jul 07, 2020
12688 Views
Jul 07, 2020
8387 Views
Jul 04, 2020
18760 Views
Jul 04, 2020
9682 Views
Jul 04, 2020
6727 Views
Jul 04, 2020
3439 Views
Jul 03, 2020
9861 Views
Jul 01, 2020
8077 Views
Jul 01, 2020
5701 Views
Jun 30, 2020
13633 Views
Jun 30, 2020
10391 Views
Jun 23, 2020
7647 Views
Jun 17, 2020
7933 Views
Jun 16, 2020
5859 Views
Jun 12, 2020
9241 Views
Jun 09, 2020
7069 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.