I'm trying to build a room booking web application with php+mysql. I'm close, but something keeps going wrong.
The way it works is this:
1. User logs in
2. User redirects to "booking" page, where they can choose from 1 of 2 rooms to book
3. there are drop downs for the following: Date, Time from, Time to
4. optional comments
5. user hits "SUBMIT" and depending on the ERROR CHECK result, either the room will be booked, or an error will be thrown.
Here is my problem (assuming date April 22nd 2010 is used):
If room [A] is booked from:
7am-9am and;
10am-1pm and;
1pm - 4pm
Everything is fine, because there are no intersections in time.
One there is an overlap in time, things get weird. Here is my code for checking and inserting the booking.
I think what's happening is that if there are more than 2 bookings in a single day, the error check just craps out and accepts anything.
*users selected the desired FROM and TO times from a drop down select, which records the time in "H:i:s" format (ie. 10:00:00).
p.s: i'm new to this and have been able to put this together with my little experience in php and lots of research online.
<?php
$userid = $_POST['userid'];
$bookTimeFrom = $_POST['bookTimeFrom'];
$bookTimeTo = $_POST['bookTimeTo'];
$meetingroom = $_POST['meetingroom'];
$bookdate = $_POST['bookdate'];
$today = $_POST['today'];
$comment = $_POST['comment'];
$fullname =$_POST['fullname'];
$conflict = false;
$bookTimeFromcv = strtotime($bookTimeFrom);
$bookTimeTocv = strtotime($bookTimeTo);
$query = mysql_query("SELECT * FROM bookingdata WHERE bookdate='$bookdate' AND meetingroom='$meetingroom'") or die(mysql_error()); // Select all the rows
while ($row = mysql_fetch_array($query)) {
$from_compare= strtotime($row['bookTimeFrom']);
$to_compare= strtotime($row['bookTimeTo']);
}
$intersect = min($bookTimeTocv, $to_compare) - max($bookTimeFromcv, $from_compare);
if ( $intersect < 0 ) $intersect = 0;
$overlap = $intersect / 3600;
$bookTimeFromcvb = date("H:i:s", ($bookTimeFromcv));
$bookTimeTocvb = date("H:i:s", ($bookTimeTocv));
if ( $overlap <= 0 and $bookTimeFromcvb!=$bookTimeTocvb) {
echo 'There are no time conflicts.';
$query = "INSERT INTO bookingdata (ID,userid,bookdate,comment,meetingroom,today,bookTimeFrom,bookTimeTo) VALUES ('','$userid','$bookdate','$comment','$meetingroom','$today','$bookTimeFromcvb','$bookTimeTocvb')";
mysql_query ($query) or die ('error updating database');
}
else {
echo "There is either a time conflict, or an invalid time selection. Please click OK to adjust time, or try another room";
}
?>