php - 无法使用sql语句删除表中的输入

I don't know what I have done to this but I had the form working and now it has stopped. My problems are with the bottom if statement where I am trying to delete the user. As I said it was working properly and now it has stopped. I just can't figure out what the issue is? And yes I do backup but clearly should do it more!

function HMdisplayrooms() {
    global $wpdb;
    echo '<html><body><h1>Display Rooms</h1>';
    echo '<p>Order room view by: <p>
    <form name = "view_HM_rooms" method="post" action="">
            <select name="roomsView" size = "1">
                <option value = "room_id">Room ID</option>
                <option value = "room_type">Room Type</option>
            </select></br>
            <input type="submit" name="action">
    </form></body></html>';
    echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room      ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
    if(isset($_POST['action'])) {
        $roomType = $_POST['roomsView'];
        $query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
        $rooms = $wpdb->get_results($query);

        foreach ($rooms as $room) {
            $roomID = ($room->room_id);
            echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
            <input type="submit" name="action2" value="Delete Room"></form></td></tr>';

            if(isset($_POST['action2'])){
                $results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
                $msg = "Room deleted";  
                return $msg;
            }
        }       
    }
}

Use the code like this,

function HMdisplayrooms() {
    global $wpdb;
    echo '<html><body><h1>Display Rooms</h1>';
    echo '<p>Order room view by: <p>
    <form name = "view_HM_rooms" method="post" action="">
            <select name="roomsView" size = "1">
                <option value = "room_id">Room ID</option>
                <option value = "room_type">Room Type</option>
            </select></br>
            <input type="submit" name="action">
    </form></body></html>';
    echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room      ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
    if(isset($_POST['action'])) {
        $roomType = $_POST['roomsView'];
        $query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
        $rooms = $wpdb->get_results($query);

        foreach ($rooms as $room) {
            $roomID = ($room->room_id);
            echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
            <input type="hidden" name="DelRooMId" value="'.$roomID.'">
            <input type="submit" name="action2" value="Delete Room"></form></td></tr>';


        }       
    }

     if(isset($_POST['action2'])){
                $roomID = $_POST['DelRooMId']; // sanitize the input
                $results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
                $msg = "Room deleted";  
                return $msg;
     }
}

I have pulled the DELETE block outside of your code. It may help you. The problem with your code is $_POST['action'] will be null at the time of DELETE operation , so the code never executes, that may be the error

Note - Make sure to get the $roomID when you are using the below structure