简体   繁体   中英

PHP / MySQL: If Else condition doesn't work as expected

Based on my question above, currently, I created a booking system. This system is to book the meeting room. In this system, admin responsible to book the meeting room after get the request from staff. Thus, admin need to key in some information such as:

1) Requester's Email

2) Room

3) Purpose

4) Start Time and End Time

The most important parameter is Room, Purpose and Start Time & End Time. For example, if Room B booked on Today from 8.00 AM to 10.00 PM, then, other staff cannot booked Room B during that time.

My problem is, although I do If else to check the parameter, it still can book the room although the date and time have clashed.

Below is my code:

<?php

require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";
$Email = $_SESSION['login_user'];
$UserID = $_SESSION['userid'];

if(isset($_POST['Submit'])) {   

    require '../../../config/PHPMailer/Mailer/Exception.php';
    require '../../../config/PHPMailer/Mailer/PHPMailer.php';
    require '../../../config/PHPMailer/Mailer/SMTP.php';

    $Requested_by = $_POST['Requested_by'];
    $date = $_POST['date'];
    $timeFrom = $_POST['timeFrom'];
    $timeTo = $_POST['timeTo'];

    //to add %20 for space
    $from = rawurlencode($timeFrom);
    $to = rawurlencode($timeTo);

    $Meeting_Description = $_POST['Meeting_Description'];
    $Room_ID = $_POST['Room_ID'];
    $Admin_email = $_POST['Admin_email'];
    $Remark = $_POST['Remark'];
    $Book_Status = $_POST['Book_Status'];
    $StartTime = $date." ".$timeFrom;
    $EndTime = $date." ".$timeTo;

     //check start end, room no
    $url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayBookingData?timeStart=$timeTo&timeEnd=$timeFrom&Room_ID=$Room_ID&Book_No=";
    $data2 = file_get_contents($url2);
    $json2 = json_decode($data2);
    $results = $json2->bookingList;

    $url3 = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectRoom?Room_Desc=&Fac_ID=&Room_ID=$Room_ID";
    $data3 = file_get_contents($url3);
    $json3 = json_decode($data3);
    $results2 = $json3->roomList;

    $url4 = "http://172.20.0.45/TGWebService/TGWebService.asmx/getUserInfo?userID=$UserID";
    $data4 = file_get_contents($url4);
    $json4 = json_decode($data4);
    $results3 = $json4->userList;

    if(empty($results)){

        if (isset($results3[0])){
        $sql = "INSERT INTO booking(Requested_by, Book_Date, StartTime, EndTime, Meeting_Description, Room_ID, Admin_email, Remark, Book_Status) 
        VALUES(:Requested_by, :Book_Date, :StartTime, :EndTime, :Meeting_Description, :Room_ID, :Admin_email, :Remark, :Book_Status)";
        $query = $conn->prepare($sql);

        $query->bindparam(':Requested_by', $Requested_by);
        $query->bindparam(':Book_Date', $date);
        $query->bindparam(':StartTime', $StartTime);
        $query->bindparam(':EndTime', $EndTime); 
        $query->bindparam(':Meeting_Description', $Meeting_Description);
        $query->bindparam(':Room_ID', $Room_ID);
        $query->bindparam(':Admin_email', $Admin_email);
        $query->bindparam(':Remark', $Remark);
        $query->bindparam(':Book_Status', $Book_Status);
        $query->execute();

            if(!empty($results)){

            echo "<script>alert('Something were going wrong. Please try again.')</script>
                    <script>window.location = '../../dashboard/admin/dashboard_admin.php'</script>";
            }else{

            echo "<script>alert('Data sucessfully saved!')</script>
                    <script>window.location = '../../dashboard/admin/dashboard_admin.php'</script>";
            }

            }else{

                echo "<script>alert('Invalid email address')</script>
                    <script>window.location = 'manual_booking_admin.php'</script>";
            }

    }else{

        echo "<script>alert('The time range you are selected is conflicted with another meeting')</script>
            <script>window.location = 'manual_booking_admin.php'</script>";
    }

?>

Can I know what is problem with my code?

So, rather than using URLs to check for conflicting bookings and requiring browser refreshes and multiple round trips to the server, this solution tests against the booking table directly . The SQL SELECT could be easily modified to retrieve a list of conflicting bookings.

This code has not been tested! It may contain typos, errors, etc.

Check the code comments for info about how it works. If you have questions, ask in the comments.

<?php

require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";
$Email = $_SESSION['login_user'];
$UserID = $_SESSION['userid'];

if(isset($_POST['Submit'])) {   

    require '../../../config/PHPMailer/Mailer/Exception.php';
    require '../../../config/PHPMailer/Mailer/PHPMailer.php';
    require '../../../config/PHPMailer/Mailer/SMTP.php';

    $Requested_by = $_POST['Requested_by'];
    $date = $_POST['date'];
    $timeFrom = $_POST['timeFrom'];
    $timeTo = $_POST['timeTo'];

    //to add %20 for space
    $from = rawurlencode($timeFrom);
    $to = rawurlencode($timeTo);

    $Meeting_Description = $_POST['Meeting_Description'];
    $Room_ID = $_POST['Room_ID'];
    $Admin_email = $_POST['Admin_email'];
    $Remark = $_POST['Remark'];
    $Book_Status = $_POST['Book_Status'];
    $StartTime = $date." ".$timeFrom;
    $EndTime = $date." ".$timeTo;


    //  The sql statement could be on one line, but this reads more clearly, and can be echoed easily.
    //  The two expressions  after the room id comparisons test for four conditions.
    //  The first check to see if the new booking is within an existing booking.
    //  The second checks for the other three possibilities:
    //      1.  Starts before booking and ends within booking.
    //      2.  Starts within a booking and ends after the booking.
    //      3.  Starts before a booking and ends after the booking.
    $sql = "";
    $sql .= "SELECT\n";
    $sql .= "   COUNT(*) as `NumConflicts`\n";
    $sql .= "   FROM `booking`\n";
    $sql .= "   WHERE `Book_Date` = :date AND\n";
    $sql .= "       :Room_ID = `Room_ID` AND\n";
    $sql .= "       (\n";
    $sql .= "           (:timeTo_1 BETWEEN `StartTime` AND `EndTime` AND :timeFrom_1 BETWEEN `StartTime` AND `EndTime`) OR\n";
    $sql .= "           (`StartTime` BETWEEN :timeFrom_2 AND :timeTo_2 OR `EndTime` BETWEEN :timeFrom_3 AND :timeTo_3)\n";
    $sql .= "       );";

    $query_1 = $conn->prepare($sql);

    //  For the timeTo and timeFrom values in the SQL statement, they are split into 3 separate 
    //  IDs since an ID can only appear once in a statement to be prepared.
    $query_1->bindparam(':date', $date);
    $query_1->bindparam(':Room_ID', $Room_ID);
    $query_1->bindparam(':timeTo_1', $timeTo);
    $query_1->bindparam(':timeFrom_2', $timeFrom); 
    $query_1->bindparam(':timeTo_2', $timeTo);
    $query_1->bindparam(':timeFrom_3', $timeFrom); 
    $query_1->bindparam(':timeTo_3', $timeTo);
    $query_1->bindparam(':timeFrom_1', $timeFrom); 

    if()$query_1->execute()) {
        if($query_1->rowCount() => 1) {
            $row = $query_1->fetch(PDO::FETCH_ASSOC);
            if($row !== false) {
                if($row['NumConflicts'] >= 1) {
                    //  Conflicting Booking(s) Exist.
                    echo "<script>alert('There is at least one other booking that overlaps this booking. Please try again.')</script>
                        <script>window.location = '../../dashboard/admin/dashboard_admin.php'</script>";
                } else {
                    //  No Conflicting Bookings.

                    $sql = "INSERT INTO `booking` (`Requested_by`, `Book_Date`, `StartTime`, `EndTime`, `Meeting_Description`, `Room_ID`, `Admin_email`, `Remark`, `Book_Status`) 
                    VALUES(:Requested_by, :Book_Date, :StartTime, :EndTime, :Meeting_Description, :Room_ID, :Admin_email, :Remark, :Book_Status)";
                    $query_2 = $conn->prepare($sql);

                    $query_2->bindparam(':Requested_by', $Requested_by);
                    $query_2->bindparam(':Book_Date', $date);
                    $query_2->bindparam(':StartTime', $StartTime);
                    $query_2->bindparam(':EndTime', $EndTime); 
                    $query_2->bindparam(':Meeting_Description', $Meeting_Description);
                    $query_2->bindparam(':Room_ID', $Room_ID);
                    $query_2->bindparam(':Admin_email', $Admin_email);
                    $query_2->bindparam(':Remark', $Remark);
                    $query_2->bindparam(':Book_Status', $Book_Status);
                    if($query_2->execute()) {
                        echo "<script>alert('Booking successfully saved!')</script>
                            <script>window.location = '../../dashboard/admin/dashboard_admin.php'</script>";
                    } else {
                        echo "<script>alert('Something went wrong saving the booking. Please try again.')</script>
                            <script>window.location = '../../dashboard/admin/dashboard_admin.php'</script>";
                    }
                }
            }
        }
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM