PHP MySQL Delete Record with Confirmation

If you are displaying any data in the list. And you want to add a delete button to the list and when a user clicks on the delete button then the confirmation box should open. Data or record is deleted from the database only when the end user confirms.

In this tutorial, you will learn how to create a confirmation box and functionality to delete records from a database in PHP Mysql.

PHP MySQL Delete Record with Confirmation

Steps to delete record or data from MySQL database with confirmation in PHP MySQL:

  • Step 1: Create the Database and Table
  • Step 2: Create a Connection File
  • Step 3: Display the Records and Add Delete Links
  • Step 4: Create the Delete Confirmation PHP File
  • Step 5: Test the Application

Step 1: Create the Database and Table

First of all, open your phpmyadmin and execute the following query to create database and table for your php project:

CREATE DATABAE DEMO;
CREATE TABLE users (
    id INT(11) PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    email VARCHAR(100),
    phone VARCHAR(15)
);

Step 2: Create a Connection File

Now, you need to create database connection file, which is used to connect mysql database from your php project. So, Create a file named “connection.php” and add the following code to establish a connection with your MySQL database:

<?php
$host = 'localhost'; // MySQL server hostname
$db = 'mydatabase'; // Your database name
$user = 'root'; // MySQL username
$password = ''; // MySQL password

// Establish the database connection
$conn = mysqli_connect($host, $user, $password, $db);

// Check the connection
if (!$conn) {
    die('Connection failed: ' . mysqli_connect_error());
}
?>

Step 3: Display the Records and Add Delete Links

Now, create PHP file, which is used to show records and delete buttons. So, create one file named “index.php”. And add the following code to it:

<?php include 'connection.php'; ?>

<!DOCTYPE html>
<html>
<head>
    <title>Users List</title>
</head>
<body>
    <?php
    // Retrieve all records from the database
    $query = "SELECT * FROM users";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) > 0) {
        echo '<h2>Users List</h2>';
        while ($row = mysqli_fetch_assoc($result)) {
            echo '<p>Name: ' . $row['name'] . '</p>';
            echo '<p>Email: ' . $row['email'] . '</p>';
            echo '<a href="delete.php?id=' . $row['id'] . '">Delete</a>';
            echo '<hr>';
        }
    } else {
        echo 'No records found.';
    }
    ?>

</body>
</html>

Step 4: Create the Delete Confirmation PHP File

Create a new PHP file, let’s name it “delete.php”. This file will contain the logic to delete a record from the database.

<?php include 'connection.php'; ?>

<!DOCTYPE html>
<html>
<head>
    <title>Delete Record Confirmation</title>
</head>
<body>
    <?php
    // Check if a record ID is provided
    if (isset($_GET['id'])) {
        $id = $_GET['id'];

        // Retrieve the record from the database
        $query = "SELECT * FROM users WHERE id = $id";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_assoc($result);

        // Display the record details and confirmation form
        if ($row) {
            echo '<h2>Are you sure you want to delete this record?</h2>';
            echo '<p>Name: ' . $row['name'] . '</p>';
            echo '<p>Email: ' . $row['email'] . '</p>';
            echo '<form method="post" action="delete.php">';
            echo '<input type="hidden" name="id" value="' . $id . '">';
            echo '<input type="submit" name="confirm" value="Yes">';
            echo '<a href="index.php">No</a>';
            echo '</form>';
        } else {
            echo 'Record not found.';
        }
    } else {
        echo 'Invalid record ID.';
    }

    // Handle the delete operation
    if (isset($_POST['confirm'])) {
        $id = $_POST['id'];

        // Delete the record from the database
        $query = "DELETE FROM users WHERE id = $id";
        $result = mysqli_query($conn, $query);

        if ($result) {
            echo 'Record deleted successfully.';
        } else {
            echo 'Error deleting record: ' . mysqli_error($conn);
        }
    }
    ?>

</body>
</html>

Step 5: Test the Application

That’s it! Now, when you run the “index.php” file, it will display the records from the “users” table with a “Delete” link next to each record. Clicking on the “Delete” link will prompt a confirmation dialog. If you confirm the deletion, the record will be deleted from the database.

Please make sure to replace “your_username”, “your_password”, and “your_database” with your MySQL database credentials in both PHP files.

Conclusion

That’s it; you have learned how to delete records or data from MySQL database with confirmation in php mysql.

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *