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

时间:2018-01-30 19:37:23

标签: php mysql sql

几个星期前我开始学习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>

2 个答案:

答案 0 :(得分:0)

你好我很乐意帮助你:) 你在这里弄错了。

Startup.Auth.cs

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

我希望你明白这一点。

答案 1 :(得分: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.")";