'delete multiple records from wordpress database with multiple checkbox

I created a customer table in Wordpress database that name "tblvessel". The code below select the records from the database and display the records as a table with the "checkbox" on each records, and also assign the record's 'ID' to each the "checkbox" value.

I want user able to delete multiple records from wordpress database by checking the 'checkbox'.

<div class="container" style="padding-left: 0">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th></th>
                <th>ETA</th>
                <th>Vessel type</th>
                <th>Vessel</th>             
                <th>Flag</th>
                <th>Origin</th>
                <th>Destination</th>
                <th>Arrival/Departure</th>
                <th>Remark</th>
                <th>User</th>
            </tr>
        </thead>
        <tbody>
            <?php
            global $wpdb;
            $today  =   date('Ymd');

            $todayuploads = $wpdb->get_results ( "SELECT * FROM tblvessel WHERE upload_date = $today ORDER BY date DESC, type, vessel, av_dp ASC" );

            foreach ( $todayuploads as $todayupload )   {

                $id = $todayupload->id;
                ?>
                <tr>
                    <td><input type="checkbox" name="vschbox[]" value="<?php echo $id; ?>"></td>
                    <td><?php echo date_format(date_create($todayupload->date), 'd/m/Y' ); ?> <?php echo $todayupload->eta_etd; ?></td>
                    <td>
                        <?php 
                        $conv_type = $todayupload->type;
                        switch ($conv_type) {
                            case 1:
                               echo "Container Barges";
                               break;
                            case 2:
                               echo "General Cargo Barges";
                               break;
                            case 3:
                               echo "Tourist Vessel";
                               break;
                            case 4:
                               echo "Motor Tanker";
                               break;                           
                            default:
                                echo "Invalid Vessel Type";
                        }
                        ?>                      
                    </td>
                    <td><?php echo $todayupload->vessel;?></td>
                    <td><?php echo $todayupload->flag;?></td>
                    <td><?php echo $todayupload->origin;?></td>
                    <td><?php echo $todayupload->destination;?></td>
                    <td>
                        <?php 
                        $conv_avdp = $todayupload->av_dp;
                        switch ($conv_avdp) {
                            case 1:
                               echo "Arrival";
                               break;
                            case 2:
                               echo "Departure";
                               break;                                       
                            default:
                                echo "Invalid Arrival/Departure Type";
                        }
                        ?>                      
                    </td>
                    <td><?php echo $todayupload->remark;?></td>
                    <td><?php echo $todayupload->user;?></td>
                </tr>
                <?php 
            } 
            ?>
        </tbody>
    </table>
    <form action="" method="post" id="deletedataform">
        <input type="submit" name="delete" value="Delete" class="btn btn-info pull-right" style="width:100px; margin-bottom:10px;" />
    </form>

</div>

<?php
global $wpdb;
if(isset($_POST['delete'])){
    $checkbox = $_POST['vschbox'];
    for($i=0;$i<count($checkbox);$i++){
        $del_id = $checkbox[$i]; 

        $message = "Data deleted successfully !";
    }
}
?>


Solution 1:[1]

It can be like this

<?php
global $wpdb;
$ids=[1,4,7,9];
$ids = implode(',', $ids);
$wpdb->query( 
    "DELETE FROM tblvessel
     WHERE id IN($ids)"     
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AbdulAhmad Matin