0

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;
            }
        }       
    }
}
5
  • 1
    You have your form and delete query set inside a foreach loop. You sure you want to do that? Commented May 28, 2014 at 3:50
  • can you post the error or warning msg. If any thing is coming. Commented May 28, 2014 at 3:52
  • I don't know php and am not a very good coder, only just started. I was simply making a table using php and it needed a form by it. I just want to delete the room that has the associated delete submit button. It was working, but I don't know how I broke it. Commented May 28, 2014 at 3:53
  • 1
    when you click Delete Room $_POST['action2'] will be set , but $_POST['action'] will be null that time. Try checking that. @Fred-ii- you are right, the block should be made stand alone Commented May 28, 2014 at 3:53
  • No error message is coming up. Commented May 28, 2014 at 3:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I know what your saying but it was working so that is why I know I had it right.
Made some edits to the initial code, compare it with yours, may help you
OMG thank you. I don't know what I did before to make it work before, but this is much more elegant. I did not know about the hidden code.
I was glad to be able to help you. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.