PHP:如何同时停止两台计算机的预订?

问题描述 投票:-2回答:2

几个星期前我开始学习PHP,我很高兴地说我已经制作了一个有效的计算机预订系统,我发现开发一个项目可以帮助我学习最好的,所以这就是我所做的。

注册/登录工作完美,删除帐户/更改密码等,您可以预订计算机。但是,我不知道如何在预定特定计算机时,比如PC-1,另一个用户无法再次预订,我显然需要某种IF声明,但我不知道如何去做吧。当用户预订PC-1时,它必须是这样的,该计算机不能再被预订并且回复时出错,告诉他们选择不同的计算机。我可以实现一个队列系统,但也许有点太先进了。

非常感谢任何帮助,如果我做错了格式,我道歉。

我会发布下面的代码,干杯。

<?php

    include 'header.php';
    include 'db.php';
?>

    <?php

        if (isset($_SESSION['studentId'])) {
            echo "<h3>Hello, <h3>".$_SESSION['studentId'];
        //echo "<br><br>It is currently " . date("l jS \of F Y h:i:s A");
        } else {
            echo "You are not logged in";
        }
    ?>

    <form name="book" method="GET" action="booking.php">

    <h4>Select a Computer: </h4>

    <select name="computerId"> <br>

            <?php 

                for($i=1; $i<=100; $i++)
                {

                    echo "<option name=computerId value=".$i.">PC-".$i."                
</option>";
            }
                echo "<br></br>"
            ?>

    </select>

    <h4>Select a Date: </h4>
    <input type="date" name="arrivalDate">

    <?php

        $arrivalDate = $_GET['arrivalDate'];
        $date = date('Y-m-d')
    //If ($arrivalDate = $date) {

        //  echo "Error, booking date must be in the future!";

    //  } elseif ($arrivalDate < $date) {

    //      echo "Error, booking date cannot be in the past!";

        //}

    ?>

     <h4>Select a Time: </h4>
    <input type="time" name="arrivalTime">

    <h4>What time will you finish?</h4>
    <input type="time" name="departureTime">

    <?php
            $arrivalTime = $_GET['arrivalTime'];
            $departureTime = $_GET['departureTime'];

    if ($arrivalTime = $departureTime) {

        //echo 'Error, you cannot book a computer at the same time you 
 finish with it.';

    } elseif ($arrivalTime > $departureTime) {

        echo 'Error, you cannot book a computer for a time in the past';

    }

    ?>

    <br> <br><input type="submit" name="submit" onclick="alert('Successful 
 Booking!')" value="Book">
   </form>



<?php

if(isset($_GET['submit']))
{
    $computerId = $_GET['computerId'];
    $studentId = $_SESSION['studentId'];
    $arrivalDate = $_GET['arrivalDate'];
    $arrivalTime = $_GET['arrivalTime'];
    $departureTime = $_GET['departureTime'];
    $bookingId = $studentId.'-'.$computerId;

    if($_GET['computerId'] = $_SESSION['computerId']) {

    echo 'You cannot book a computer that is already booked!';

}

    $sql = "INSERT INTO Booking (bookingDate, bookingId, studentId, 
   computerId, arrivalDate, arrivalTime, departureTime) values (CURDATE(), 
  '$bookingId', '$studentId', '$computerId', '$arrivalDate', '$arrivalTime', 
  '$departureTime')";

    if ($conn->query($sql)=== TRUE) {
        echo "<br>".$computer."";
    } else {
       // echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $sql1 = "SELECT FROM Booking '$computerId', '$arrivalDate', 
   '$arrivalTime', '$departureTime'";

    echo ' <br> You have booked computer '.$computerId.' for 
   '.$arrivalDate.' at '.$arrivalTime.' until '.$departureTime;

    $conn->close();
    }

    //$sql = "SELECT computerId FROM Booking WHERE studentId = 
    '$_SESSION['studentId']"

    //echo "hi";

   ?>

 </body>
 </html>
php mysql sql
2个回答
0
投票

您好我很乐意帮助您:)您在这里犯了一个错误。

if($_GET['computerId'] = $_SESSION['computerId']) {
   echo 'You cannot book a computer that is already booked!';
}

您如何看待会话。它为同一台计算机生成的内容不是为服务器访问的。你只需要从数据库验证。并返回此消息。当有一本书时,你需要将所有信息存储在数据库中。当其他计算机上的其他人通过数据库进行预订验证并显示此消息。因为会话无法帮助你。

我希望你明白这一点。


0
投票

您应该在代码中执行此操作,在该代码中,您将计算机连接计算机表的计算机检索到预订表,如下所示:

$now = time();//assuming you store time as Unix timestamp
$query = "SELECT * from computers 
WHERE  computers.id NOT IN 
(SELECT c.id from computers c LEFT JOIN 
bookings b ON c.id = b.computerId 
WHERE b.departureTime >  ".$now.")";
© www.soinside.com 2019 - 2024. All rights reserved.