在我的数据库php

时间:2015-04-28 16:58:59

标签: php mysql pdo

我有一个预订表格,用户可以在某个日期和两次之间预订某个设施。我已经到了这样的程度,我的PHP代码将阻止用户在相同的日期和相同的开始或结束时间预订相同的音高。

然而,用户可以在一段时间内和另一个用户在同一天预订同一设施。例如如果一个用户在5月1日从9到12预订,则另一个用户可以进入并预订10或11个插槽。

有没有办法阻止用户在另一个用户已经预订的时间之间预约???

    <?php
        include "config.php"; 
        //Booking point
        if(isset($_POST['booking']))
        {
            //get values for variables
            $pitchID = $_POST['pitchID'];
            $start_date = $_POST['start_date'];
            $start_hour = $_POST['start_hour'];
            $end_hour = $_POST['end_hour'];
            $booking_age = $_POST['booking_age'];
            $pitch_size = $_POST['pitch_size'];
            $light_tokens = $_POST['light_tokens'];


/*          $q = $db->prepare("SELECT * FROM booking WHERE start_date = ?, start_hour = ?, end_hour=?, pitchID=?");
            $query = $q->execute(array($start_date, $start_hour, $end_hour, $pitchID));
            $count = $q->rowCount(); */
                $q = $db->prepare("SELECT * FROM booking WHERE start_date = ? AND start_hour = ? AND pitchID = ?");
                $query = $q->execute(array($start_date, $start_hour, $pitchID));
                $count = $q->rowCount();

                if($count == 0){

                    $query = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?, userID='$userID'");
                    $query = $query->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));

                    if($query){
                        echo "Your booking has been made";
                        header("Location:home2_template.html");
                        return; 
                    } else {
                        echo "Fail";
                    } //else fail   
                } else {
                    echo "This booking already exists"; 
                } //else count 
        } //if booking
    ?>

1 个答案:

答案 0 :(得分:1)

如果您只能预订一天,请尝试此

select * from booking
where (start_date='new_date' 
        and ( 
                (start_hour <='new_start_hour' and end_hour>='new_start_hour') 
                or
                (start_hour<= 'new_end_hour' and end_hour>='new_end_hour')
            )
    ) and pitchID = ?