How to Delete Multiple Records in MySQL Using PHP

Mar 15, 2022 . Admin



Hello dev,

I am going to explain you How To Delete Multiple Records in MySQL Using PHP. You will learn how to Delete Multiple Records From MySQL Using PHP. In side this article we will see how to Delete Multiple Records from MySQL Database in PHP.

This article will give you simple example of How to delete multiple rows from MYSQL table using PHP. We will use get simple Delete Multiple Selected Records with PHP.

I will give you simple Example Delete multiple records with PHP and MySql.

So, let's see bellow solution:

conn.php
<?php 
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "db_php";

    $conn = new mysqli($servername,$username,$password,$dbname);

    if($conn->connect_error){
        die ('connection faild:'.$conn->connect_error);
    }
?>
index.php
<?php 
    session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <title>How To Delete Multiple Records in MySQL Using PHP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-dark">
    <div class="container mt-5">
        <?php 
            if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"]))
                {
                    $msg=$_SESSION["msg"];
                    echo "<div class='alert alert-danger' role='alert'>".$msg."</div>";
                    unset($_SESSION['msg']);
                    session_destroy();
                }
        ?>
        <div class="card">
            <div class="card-header">
                <h3 class="text-center">How To Delete Multiple Records in MySQL Using PHP - Mywebtuts.com</h3>
            </div>
            <div class="card-body">
                <form method="POST" action="delete.php">
                    <table class="table table-striped table-bordered table-hover text-center">
                        <tr>
                            <th width="10px">#</th>
                            <th width="10px">id</th>
                            <th width="50%">name</th>
                            <th>email</th>
                        </tr>
                        <?php
                            include('conn.php');
             
                            $query=mysqli_query($conn,"select * from `users`");
                            while($row=mysqli_fetch_array($query)){
                        ?>
                        <tr>
                            <td align="center"><input type="checkbox" value="<?php echo $row['id']; ?>" name="id[]"></td>
                            <td><?php echo $row['id']; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['email']; ?></td>     
                        </tr>
                        <?php
                            }
                        ?>
                    </table>
                    <button type="submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>Delete</button>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
delete.php
<?php
    session_start();
    include('conn.php');

    if(isset($_POST['id'])){
        $checkedId = $_POST['id'];
        $deleteMsg = deleteMultipleData($conn, $checkedId);
    }
    function deleteMultipleData($conn, $checkedId){

        $checkedIdGroup = implode(',', $checkedId);
        $query = "DELETE FROM users WHERE id IN ($checkedIdGroup)";
        $result = $conn->query($query);
        if($result==true){
            $_SESSION['msg']= "Selected data was deleted successfully";
            header("location:index.php");
        }
    }
?>
Output:

It will help you...
#PHP