douhuan6157
douhuan6157
2018-08-10 07:33

使用多个复选框从wordpress数据库中删除多个记录

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 !";
    }
}
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • droirqk4795 droirqk4795 3年前
    ********Big Thanks to "tilz0r" in the comments.**********
    ***I'm new here, don't know how to close my asked question, so I'll leave a answer here.***
    **In case "tilz0r" post the answer, I'll Mark "tilz0r" post as the correct answer**
    
    <?php
    global $wpdb;
    if(isset($_POST['delete'])){
        $checkbox = $_POST['vschbox'];
        for($i=0;$i<count($checkbox);$i++){
            $del_id = $checkbox[$i]; 
            $wpdb->query( 
                "DELETE FROM tblvessel
                 WHERE id IN($del_id)
                "       
            );
    
            $message = "Data deleted successfully !";
        }
    }
    ?>
    
    <div class="container" style="padding-left: 0">
        <form action="" method="post" id="deletedataform">
            <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>
    
            <input type="submit" name="delete" value="Delete" class="btn btn-info pull-right" style="width:100px; margin-bottom:10px;" />
        </form>
    
    </div>
    
    点赞 评论 复制链接分享

为你推荐