Delete Multiple Rows Using Checkbox In PHP/MySQLi | CampCodes

Delete Multiple Rows using Checkbox in PHP/MySQLi

January 14, 2021
PHP
delete multiple rows using checkbox in php

Tutorial: How to Delete Multiple Rows using Checkbox in PHP/MySQLi

About Delete Multiple Rows using Checkbox in PHP

How to delete multiple rows using the checkbox in PHP/MySQLi is this tutorial all about. I’ve to use bootstrap and modal in this tutorial but take note that they are hosted so need an internet connection for them to work.

Creating a Database

The first step is to create our database.

1. Open phpMyAdmin.

2. Click databases, create a database and name it as “sample”.

3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

CREATE TABLE `user` (
  `userid` INT(11) NOT NULL AUTO_INCREMENT,
  `firstname` VARCHAR(30) NOT NULL,
  `lastname` VARCHAR(30) NOT NULL,
  `address` VARCHAR(100) NOT NULL,
PRIMARY KEY(`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Creating a Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.

<?php
 
//MySQLi Procedural
$conn = mysqli_connect("localhost","root","","sample");
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
 
?>

index.php

This is the page where we show our sample table. Also, for you to further practice the delete function, I’ve added Add New function.

<!DOCTYPE html>
<html>
<head>
    <title>PHP/MySQLi Deleting Multiple Rows using Checkbox</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
    <style>
        input[type="checkbox"] {
        transform:scale(2, 2);
    }
    </style>
</head>
<body>
<div class="container">
    <div style="height:50px;"></div>
    <div class="well" style="margin:auto; padding:auto; width:80%;">
    <span style="font-size:25px; color:blue"><center><strong>PHP/MySQLi Deleting Multiple Rows using Checkbox</strong></center></span>	
        <div style="height:20px;"></div>
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <th></th>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Address</th>
 
            </thead>
            <form method="POST" action="delete.php">
            <tbody>
            <?php
                include('conn.php');
 
                $query=mysqli_query($conn,"select * from `user`");
                while($row=mysqli_fetch_array($query)){
                    ?>
                    <tr>
                        <td align="center"><input type="checkbox" value="<?php echo $row['userid']; ?>" name="userid[]"></td>
                        <td><?php echo $row['firstname']; ?></td>
                        <td><?php echo $row['lastname']; ?></td>
                        <td><?php echo $row['address']; ?></td>		
                    </tr>
                    <?php
                }
 
            ?>
            </tbody>
        </table>
            <a href="#addnew" data-toggle="modal" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Add New</a> || <button type="submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button>
            </form>
    </div>
    <?php include('modal.php'); ?>
</div>
</body>
</html>

modal.php

This is the modal that contains our Add New form.

<!-- Add New -->
    <div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <center><h4 class="modal-title" id="myModalLabel">Add New</h4></center>
                </div>
                <div class="modal-body">
                <div class="container-fluid">
                <form method="POST" action="addnew.php">
                    <div class="row">
                        <div class="col-lg-2">
                            <label class="control-label" style="position:relative; top:7px;">Firstname:</label>
                        </div>
                        <div class="col-lg-10">
                            <input type="text" class="form-control" name="firstname">
                        </div>
                    </div>
                    <div style="height:10px;"></div>
                    <div class="row">
                        <div class="col-lg-2">
                            <label class="control-label" style="position:relative; top:7px;">Lastname:</label>
                        </div>
                        <div class="col-lg-10">
                            <input type="text" class="form-control" name="lastname">
                        </div>
                    </div>
                    <div style="height:10px;"></div>
                    <div class="row">
                        <div class="col-lg-2">
                            <label class="control-label" style="position:relative; top:7px;">Address:</label>
                        </div>
                        <div class="col-lg-10">
                            <input type="text" class="form-control" name="address">
                        </div>
                    </div>
                </div> 
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</a>
                </form>
                </div>
 
            </div>
        </div>
    </div>

addnew.php

Our code in adding new row to our MySQL Table.

<?php
    include('conn.php');
 
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $address=$_POST['address'];
 
    mysqli_query($conn,"insert into user (firstname, lastname, address) values ('$firstname', '$lastname', '$address')");
    header('location:index.php');
 
?>

delete.php

Lastly, our delete code. By pressing the delete button, this code will delete all checked rows.

<?php
    include('conn.php');
 
    if(isset($_POST['userid'])){
        foreach ($_POST['userid'] as $id):
            mysqli_query($conn,"delete from user where userid='$id'");
        endforeach;
 
        header('location:index.php');
    }
    else{
        ?>
        <script>
            window.alert('Please check user to Delete');
            window.location.href='index.php';
        </script>
        <?php
    }
 
?>

And that ends this tutorial. If you have any comments or questions, feel free to message me or comment below. Hope this helps. Happy Coding!

Icon

Delete Multiple Rows using Checkbox in PHP/MySQLi Source Code 29.12 KB 804 downloads

About Delete Multiple Rows using Checkbox in PHP How to delete multiple rows using...

 

https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

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