当我按下按钮删除时,从MySQL表中删除行

时间:2016-02-14 07:43:50

标签: php html mysql

我需要为一位能看到所有房间预订的用户取消预订房间。 问题是它显示了用户预订的房间,但删除按钮无法正常工作。

我有我的数据库here和屏幕截图here的快照。请帮忙吗?

感谢。

这是我的show code数据的php代码和manage.php中的删除部分

<?php

session_start();
$connection = mysqli_connect('localhost', 'root', '', 'webapp');

global $connection;


$user_id="";
if(!isset($_SESSION['login_ID'])) {
header('Location:  login.php'); 
die();
} else {
    $user_id= $_SESSION['login_ID'];
}


    $secondQueryStmt = "SELECT * FROM `booking` where UserID='".$user_id."'";
    $query = mysqli_query($connection, $secondQueryStmt);

    if(isset($_POST['BookingID'])) {
    $boking_id = $_POST['BookingID'];
    if(isset($_POST['delete_id'])){




        $query = mysqli_query($connection, "DELETE FROM booking where BookingID= '".$boking_id."'");
    }
}

&GT?; 这是我在同一页面中的表格html。

<article id="content" style="height:810px; background-color:white">
      <div class="box1">
      <!--content herer-->








                    <table class="wrapper" style="text-align:center">
                    <tr>
                    <th class="track animated fadeInUp">Room</th>
                    <th class="title animated fadeInUp">Quantity</th> 
                    <th class="speaker animated fadeInUp">CheckIn Date</th>
                    <th class="day animated fadeInUp">CheckOut Date </th>
                    <th class="time animated fadeInUp"></th>

                    </tr>
                        <?php while($fetcher = mysqli_fetch_array($query)): ?>
                        <tr>
                        <td><?=$fetcher['Type']?></td>
                        <td><?=$fetcher['Quantity']?></td> 
                        <td><?=$fetcher['CheckInDate']?></td>
                        <td><?=$fetcher['CheckOutDate']?></td>
                        <td><td><form action="manage.php"  method= "post" />
                    <input  type="hidden" name="b_id" value="<?=$fetcher['BookingID'] ?>"/>
                    <input type="button"  class="btnSm hvr-fade lightRed" name="delete_id" value="Cancel Booking"/>
                </form></td></tr>



                 <?php endwhile; ?>




        </table>


        </div>
    </article>

1 个答案:

答案 0 :(得分:0)

请看这里的陈述,

<input  type="hidden" name="b_id" value="<?=$fetcher['BookingID'] ?>"/>
                             ^ you've named it b_id, not BookingID

所以改变

$boking_id = $_POST['BookingID'];

$boking_id = $_POST['b_id'];

并使用mysqli_affected_rows()函数检查此UPDATE操作会影响的行数。

以下是参考资料:

所以你的代码应该是这样的:

// your code

$query = mysqli_query($connection, $secondQueryStmt);

if(isset($_POST['delete_id'])){
    $boking_id = $_POST['b_id'];
    mysqli_query($connection, "DELETE FROM booking where BookingID= '".$boking_id."'");
    if(mysqli_affected_rows($connection)){
        // success
    }else{
        // failure
    }
}