How to Delete Multiple Records From MySQL Using PHP

So here i will tell you how to delete bulk records from MySQL table in PHP with checkbox, you just need to select checkbox which one you want to delete and hit submit button to delete. As we know, Its very time consuming to delete records one by one. Everyone wants to delete bulk record on single event only. You may also like How to Add, Edit and Delete Records Using jQuery, Ajax, PHP and MySQL.

PHP and HTML Code

<?php
    define('DB_SERVER', "localhost");
    define('DB_USER', "root");
    define('DB_PASS', "");
    define('DB_DATABASE', "ieltsmedidb");
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
?>

<?php
if(isset($_POST['delete_records']) && count($_POST['no'])>0)
{
    for($i=0;$i<count($_POST['no']);$i++)
    {
        $row_no=$_POST['no'][$i];
        mysqli_query($con,"delete from medical_student where student_id = '$row_no' ");
    }
}
?>

<html>
<head>
</head>
    <body>
        <?php
        $select = mysqli_query($con, "SELECT * FROM medical_student");
        ?>
        <form method="post" action="">
            <table align="center" cellpadding="10" border="1">
                <tr>
                    <th></th>
                    <th>NAME</th>
                    <th>AGE</th>
                </tr>
                <?php
                while ($row = mysqli_fetch_assoc($select)) {
                    echo "<tr>";
                    echo "<td><input type='checkbox' name='no[]' value='" . $row['student_id'] . "'></td><td>" . $row['student_name'] . "</td><td>" . $row['email'] . "</td>";
                    echo "</tr>";
                }
                ?>
            </table>
            <input type="submit" name="delete_records" value="Delete Records">
        </form>
    </body>
</html>

Leave a Reply

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