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 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取