dongzhazhuo0572 2014-05-28 03:46
浏览 50
已采纳

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;
            }
        }       
    }
}
  • 写回答

1条回答 默认 最新

  • duanshan1856 2014-05-28 04:00
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号