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
104 Views
Aug 31, 2026
179 Views
Jun 27, 2026
286 Views
Jun 24, 2026
219 Views
Jun 23, 2026
212 Views
Jun 10, 2026
222 Views
Jun 09, 2026
282 Views
Jun 08, 2026
193 Views
May 28, 2026
282 Views
May 14, 2026
328 Views
May 02, 2026
318 Views
Apr 26, 2026
414 Views
Apr 21, 2026
769 Views
Apr 19, 2026
281 Views
Apr 15, 2026
291 Views
Mar 23, 2026
1410 Views
Mar 16, 2026
1480 Views
Mar 09, 2026
605 Views
Mar 02, 2026
448 Views
Feb 25, 2026
906 Views
Feb 18, 2026
1497 Views
Feb 09, 2026
522 Views
Jan 31, 2026
585 Views
Jan 10, 2026
6712 Views
Dec 30, 2025
689 Views
Dec 20, 2025
733 Views
Dec 04, 2024
2380 Views
Nov 29, 2024
1949 Views
Nov 25, 2024
1677 Views
Nov 18, 2024
3714 Views
Nov 13, 2024
2473 Views
Nov 11, 2024
1996 Views
Nov 06, 2024
2377 Views
Nov 04, 2024
1824 Views
Oct 30, 2024
2482 Views
Oct 28, 2024
2434 Views
Oct 23, 2024
3425 Views
Oct 21, 2024
1977 Views
Oct 16, 2024
2738 Views
Oct 14, 2024
3335 Views
Oct 09, 2024
1901 Views
Oct 07, 2024
2075 Views
Oct 04, 2024
2942 Views
Sep 30, 2024
4558 Views
Sep 23, 2024
1841 Views
Sep 20, 2024
3176 Views
Aug 02, 2024
2089 Views
Jun 08, 2023
1607 Views
Jun 01, 2023
1768 Views
May 19, 2023
3783 Views
Jul 05, 2022
4942 Views
May 08, 2022
9099 Views
Apr 17, 2022
29201 Views
Dec 02, 2021
5469 Views
Sep 13, 2021
4774 Views
May 29, 2021
5438 Views
Apr 27, 2021
3672 Views
Apr 06, 2021
5585 Views
Apr 03, 2021
4197 Views
Mar 26, 2021
2787 Views
Mar 19, 2021
3651 Views
Mar 19, 2021
2929 Views
Mar 05, 2021
2886 Views
Feb 18, 2021
4042 Views
Feb 18, 2021
3359 Views
Feb 11, 2021
2888 Views
Nov 10, 2020
3161 Views
Sep 09, 2020
4484 Views
Aug 24, 2020
4835 Views
Aug 20, 2020
4461 Views
Aug 18, 2020
6084 Views
Jul 10, 2020
8290 Views
Jul 07, 2020
11967 Views
Jul 07, 2020
10182 Views
Jul 07, 2020
12810 Views
Jul 07, 2020
8541 Views
Jul 04, 2020
19374 Views
Jul 04, 2020
9754 Views
Jul 04, 2020
6807 Views
Jul 04, 2020
3507 Views
Jul 03, 2020
9959 Views
Jul 01, 2020
8168 Views
Jul 01, 2020
5757 Views
Jun 30, 2020
13753 Views
Jun 30, 2020
10464 Views
Jun 23, 2020
7698 Views
Jun 17, 2020
8012 Views
Jun 16, 2020
5954 Views
Jun 12, 2020
9334 Views
Jun 09, 2020
7173 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.