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


Delete row from MySQL table when I press button Delete

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

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

谢谢。

这是我的 php 代码,用于显示预订数据和管理中的删除部分.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."'");
    }
}

?>这也是我在同一页面中的表单 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>

看看这句话,

<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操作影响了多少行。

以下是参考:

  • http://php.net/manual/en/mysqli.affected-rows.php

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

// 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
    }
}