I am creating a website for a project where you are able to book driving lessons.
I have created code which (sort of) prevents double booking, however, I'm having a problem.
If there is a booking stored in the DB at 11.30-12.30, if i try to book a lesson from 11-12 it prevents me from doing so, however if I try book from 12-1, it allows me to go ahead with the booking.
The code I am using is below. Does anyone know how to solve this error? Thanks!
Expand|Select|Wrap|Line Numbers
- <?php
- include('../config.php');
- $Name = '';
- $LessonDate = '';
- $StartTime = '';
- $EndTime = '';
- if (isset($_POST['submit'])) {
- if(isset($_POST['Name'])){ $Name = $_POST['Name']; }
- if(isset($_POST['LessonDate'])){ $LessonDate = $_POST['LessonDate']; }
- if(isset($_POST['StartTime'])){ $StartTime = $_POST['StartTime']; }
- if(isset($_POST['EndTime'])){ $EndTime = $_POST['EndTime']; }
- $result = mysql_query("SELECT * FROM booking WHERE LessonDate = '".mysql_real_escape_string($LessonDate)."' AND (StartTime >= '".mysql_real_escape_string($StartTime)."' AND StartTime <= '".mysql_real_escape_string($EndTime)."')");
- if(!$result)
- {
- die(mysql_error());
- }
- if(mysql_num_rows($result) > 0)
- {
- die('<p>This date and time have already been booked. <a href="http://bytes.com/booking1.php">Please try another time</a>.</p>');
- }
- else
- {
- //insert new user data into Users table
- $sql = "INSERT INTO `booking` ( `Name`, `LessonDate` , `StartTime` , `EndTime` ) VALUES( '{$Name}', '{$LessonDate}' , '{$StartTime}' , '{$EndTime}') ";
- mysql_query($sql) or die(mysql_error());
- echo "Booking Successful.<br />";
- echo "<a href='http://bytes.com/text.php'> Send reminder text </a>";
- }
- } ?>