使用按钮更新特定的表记录

时间:2018-11-24 11:50:52

标签: php html mysql database

我正在做这个房间预订系统的事情。我正在做一个批准功能,该网站上有一张桌子,它显示房间名称,开始和结束时间(以DateTime格式)以及状态(已批准) ,每条记录的已拒绝或未决(默认)
最后一列还有两个按钮,称为批准和拒绝
我想要的是管理员单击按钮时,相应记录的状态更改为“已批准”或“已拒绝”
我的代码是:

<!doctype html>
<html>
<head>
</head>
<body>
    <form method="post">

    <?php
$con=mysqli_connect("localhost","root","","abc");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"select b.bookid,b.room,b.start,b.end,b.status
        from booking
        where b.start >= now()
        order by b.start");

$numofrows = mysqli_num_rows($result);


if($numofrows == 0){        
    echo "There're no room bookings from now";
}
else{
    echo "<table border='1'>
    <tr>
    <th>Room</th>
    <th>Starting Time</th>
    <th>Ending Time</th>
    <th>Status</th>
    <th>Approve</th>
    </tr>";

    while($row = mysqli_fetch_array($result)){
        echo "<tr>";
        echo "<td>" . $row['room'] . "</td>";
        echo "<td>" . $row['start'] . "</td>";
        echo "<td>" . $row['end'] . "</td>";
        echo "<td>" . $row['status'] . "</td>";
        echo "<td>" 
        ?> 

        <input type="submit" name="approve" value="Approve">
        <input type="submit" name="reject" value="Reject">
        <input type="hidden" name="id" value='<?php echo $row["bookid"]; ?>'/>

        <?php 
        echo "</td>";
        echo "</tr>";
        }
    echo "</table>";
}

$approve = false;
$reject = false;

if(isset($_POST["approve"])){
    $id=$_POST["id"];
    $approve = mysqli_query($con," UPDATE booking SET status='Approved' WHERE bookid = '$id' ");}

if(isset($_POST["reject"])){
    $id=$_POST["id"];
    $reject = mysqli_query($con," UPDATE booking SET status='Rejected' WHERE bookid = '$id' ");}

if($approve){
    echo "approve";
}
if($reject){
    echo "reject";}
?>

其中的记录来自数据库abc中称为预订的表。预订表中包含上述字段以及主键“ bookid”。用户无法在网站上看到“ bookid”
还有两个问题
1.按钮确实会将状态更改为其相应的字符串时,只会更改最后一条记录。无论按下哪个按钮,仅更改最后一条记录的状态而其他按钮不受影响
我想我将变量设置为错误,但我不知道如何正确设置
2.尽管按钮确实将最后一条记录的状态更改为不同的字符串,但这需要单击两次。也就是说,我必须按两次“批准”按钮才能将状态更改为“已批准”
我不知道为什么会这样

0 个答案:

没有答案