How to delete data from database by ID in PHP MySQL

By Super Admin | Jun 10, 2021 | PHP
Share : Whatsapp

https://www.fundaofwebit.com/post/how-to-delete-data-from-database-by-id-in-php-mysql

In this post, you will learn How to delete data from database by ID in PHP MySQL, So guys, now we will create a form and one input field to enter the ID to delete data in php.

We will be using Bootstrap 5 version to design the user interface.

So, Lets get started:

Step 1: Create a file named index.php and paste the below code as follows:

In this file, we have created a HTML form to delete the data by giving the ID in input box and submit button to perform the action using post method.

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Funda of Web IT</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">

                <?php 
                    if(isset($_SESSION['status']))
                    {
                        ?>
                            <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <strong>Hey!</strong> <?php echo $_SESSION['status']; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            </div>
                        <?php
                        unset($_SESSION['status']);
                    }
                ?>

                <div class="card mt-5">
                    <div class="card-header">
                        <h4>How to Delete Data from Database by ID in PHP MySQL</h4>
                    </div>
                    <div class="card-body">

                        <form action="code.php" method="POST">
                            <div class="froum-group mb-3">
                                <label for="">Deleteing Student ID</label>
                                <input type="text" name="delete_stud_id" class="form-control">
                            </div>
                            <div class="froum-group mb-3">
                                <button type="submit" name="stud_delete_btn" class="btn btn-primary">Delete Data</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Step 2: Create a file named code.php and paste the below code as follows:

<?php
session_start();
$con = mysqli_connect("localhost","root","","phptutorials");

if(isset($_POST['stud_delete_btn']))
{
    $id = $_POST['delete_stud_id'];

    $query = "DELETE FROM student WHERE id='$id";
    $query_run = mysqli_query($con$query);

    if($query_run)
    {
        $_SESSION['status'] = "Data Deleted Successfully";
        header("Location: delete-data-by-id.php");
    }
    else
    {
        $_SESSION['status'] = "Data Not Deleted";
        header("Location: delete-data-by-id.php");
    }
}

?>



Thanks for reading...