Sarahpengie Posted February 4, 2013 Share Posted February 4, 2013 Hi I am creating a website where a user can book a driving lesson. I am using mySQL and PHP. I have coded a booking system where users can book a lesson for a desired date and time, however I can't seem to find code to prevent double bookings from occurring. The code I have used in my booking form is as follows: <?php include('../config.php'); echo "<table border=1 >"; echo "<tr>"; echo "<td><b>LessonDate</b></td>"; echo "<td><b>StartTime</b></td>"; echo "<td><b>EndTime</b></td>"; echo "</tr>"; $result = mysql_query("SELECT * FROM booking") or trigger_error(mysql_error()); while($row = mysql_fetch_array($result)){ foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } echo "<tr>"; echo "<td valign='top'>" . nl2br( $row['LessonDate']) . "</td>"; echo "<td valign='top'>" . nl2br( $row['StartTime']) . "</td>"; echo "<td valign='top'>" . nl2br( $row['EndTime']) . "</td>"; echo "</tr>"; } echo "</table>"; ?> If anyone could help me to prevent double bookings occurring that would be great Thanks in advance for your help Sarah. Quote Link to comment Share on other sites More sharing options...
thara Posted February 4, 2013 Share Posted February 4, 2013 (edited) If I understood your question correctly you need to use mysql DISTINCT. When querying data from a database table, you may get duplicate records. In order to remove duplicate records, you use the DISTINCT keyword along with the SELECT statements. The syntax of the MySQL DISTINCT is as follows: SELECT DISTINCT columns FROM table_name WHERE where_conditions Edited February 4, 2013 by thara Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Unfortunately, DISTINCT won't prevent double-booking. The code posted at the top doesn't have anything to do with the problem either. To prevent double-booking you have to check if the time slots are taken, before you INSERT the data into the database. Checking on display is too late. You will also need to take racing conditions into consideration, as it would still be easy to double-book something if two people filled out the form at the same time. Transactions might help with this, or adding a "reserved" token when the user starts to fill out the form. This last solution requires that the user selects the time slot first, and then is redirected to a registration form. A bit more cumbersome for the user, but almost foolproof when it comes to race conditions. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 4, 2013 Share Posted February 4, 2013 The following query will find all slots that conflict with the proposed booking If the proposed booking is on $bdate between $bstime and $betime SELECT COUNT(*) as conflicts FROM bookings WHERE LessonDate = '$bdate' AND StartTime < '$betime' AND EndTime > '$bstime' if "conflicts" is zero it's safe to book. Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 5, 2013 Author Share Posted February 5, 2013 Thanks for all ye're help! Barand, where in my code would I insert your code? Sorry I'm a bit clueless! Thanks again! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 5, 2013 Share Posted February 5, 2013 (edited) That would go in the file that's registering the bookings, after the user has submitted the proposed times and before you save the booking to the database. As previously mentioned, the code you posted above isn't really related to this problem. As you're only showing what's already saved in the database with it. At which point it is too late to prevent the double bookings. It's the same as with writing bookings into a book, really. Before you write the appointment, you first have to check (SELECT) to see if there is an appointment already in the desired time span. If there are 0 appointments, and thus 0 rows returned from Barand's query, you know that the slot is available. Otherwise, you have to show a warning, abort the saving process, and ask for a new time. Edited February 5, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 12, 2013 Author Share Posted February 12, 2013 Thanks for your help Christian. I still a bit confused as to how to go about this. So should I change my code to something like this: <form action="../process-booking.php" method='POST'> <p><b>Select a Date:</b><br /><input type="date" value="Now" name='LessonDate'/> <p><b>Select a Start Time:</b><br /><input type='time' value="Now" name='StartTime'/> <p><b>Select an End Time:</b><br /><input type='time' value="Now" name='EndTime'/> <p><input type='submit' value='Book Now' /><input type='hidden' value='1' name='submitted' /> </form> <br> And in the process-booking.php form have some code which will check if the dates are taken? Or how should I do it? Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 12, 2013 Share Posted February 12, 2013 I think you need to talk to your instructor. If we are simply telling you what code to use and where to put it you aren't learning anything. Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 17, 2013 Author Share Posted February 17, 2013 Hi, 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 have 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, I believe it has something to do with the line of code I have made "bold". Does anyone know how to solve this error? Thanks! <?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="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='text.php'> Send reminder text </a>"; } } ?> Quote Link to comment Share on other sites More sharing options...
bleured27 Posted February 17, 2013 Share Posted February 17, 2013 (edited) please clean up your code it is messy this ("SELECT * FROM booking WHERE LessonDate = '".mysql_real_escape_string($LessonDate)."' AND (StartTime >= '".mysql_real_escape_string($StartTime)."' AND StartTime <= '".mysql_real_escape_string($EndTime)."')"); should be this $LessonDate =mysql_real_escape_string($LessonDate); $StartTime=mysql_real_escape_string($StartTime); $EndTime=mysql_real_escape_string($EndTime); ("SELECT * FROM booking WHERE LessonDate = '$LessonDate' AND (StartTime >= '$StartTime' AND StartTime <= '$EndTime' ")"); *note i didn't looked @ the content yet so i did not took the errors out of it btw plese put it between code taggs so i can read it properly [tag] like this <?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="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='text.php'> Send reminder text </a>"; } } ?> Edited February 17, 2013 by bleured27 Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 17, 2013 Author Share Posted February 17, 2013 (edited) <?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="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='text.php'> Send reminder text </a>"; } } ?> Edited February 17, 2013 by Sarahpengie Quote Link to comment Share on other sites More sharing options...
bleured27 Posted February 17, 2013 Share Posted February 17, 2013 CTRL + Q to Enable/Disable GoPhoto.it i changed my last post you might want to read that and besides that delete your last reply and click eddit. Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 17, 2013 Author Share Posted February 17, 2013 Hi, Thanks, I've cleaned it up in my actual code, but when I try to edit it or delete it on this and using ctrl + Q it appears I don't have permission to make changes. Quote Link to comment Share on other sites More sharing options...
bleured27 Posted February 17, 2013 Share Posted February 17, 2013 (edited) in my browser there stands in my browser there stands eddit in the bottom right of my posting. when i eddid my posts i dont know what prefrences you have on your accaunt..... btw that control q was an mis click i didnt noticed it until now Edited February 17, 2013 by bleured27 Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 17, 2013 Author Share Posted February 17, 2013 OK thanks Do you know how I can fix my code to prevent the double bookings from occurring? Thanks again. Quote Link to comment Share on other sites More sharing options...
bleured27 Posted February 17, 2013 Share Posted February 17, 2013 well i can look into it while im doing 6 websites @ the moment and i am only here becose i have an error so i will look into it as soon as possible. Quote Link to comment Share on other sites More sharing options...
bleured27 Posted February 17, 2013 Share Posted February 17, 2013 (edited) did you tried the mysql error? Edited February 17, 2013 by bleured27 Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted February 17, 2013 Share Posted February 17, 2013 (edited) <?PHP include('../config.php'); if($_SERVER['REQUEST_METHOD'] == 'POST') { $Name = isset($_POST['Name']) ? mysql_real_escape_string($_POST['Name']) : false ; $LessonDate = isset($_POST['LessonDate']) ? mysql_real_escape_string($_POST['LessonDate']) : false ; $StartTime = isset($_POST['StartTime']) ? mysql_real_escape_string($_POST['StartTime']) : false ; $EndTime = isset($_POST['EndTime']) ? mysql_real_escape_string($_POST['EndTime']) : false ; if(empty($Name) || empty($LessonDate) || empty($StartTime) || empty($EndTime)) { echo '1 or more of the required fields are missing.'; } else { $checkBookingQuery = "SELECT `LessonDate` FROM `booking` WHERE `LessonDate` = {$LessonDate} AND (StartTime >= '{$StartTime}' AND StartTime <= '{$EndTime}')"; $checkBooking = mysql_query($checkBookingQuery); if(mysql_num_rows($checkBooking)) { echo 'This booking slot has already been taken. <a href="booking1.php">Please select a different slot</a>'; } else { $insertBookingQuery = "INSERT INTO `booking` (`Name`, `LessonDate`, `StartTime`, `EndTime`) VALUES('{$Name}', '{$LessonDate}', '{$StartTime}', '{$EndTime}')"; $insertBooking = mysql_query($insertBookingQuery); if(!mysql_affected_rows()) { echo 'Unable to insert booking.'; } else { echo 'Booking has been successfully place. <br> <a href="text.php">Send reminder text?</a>'; } } } } ?> Edited February 17, 2013 by PaulRyan Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 17, 2013 Share Posted February 17, 2013 Please keep your requests for help on thus subject in one thread. Do not double post! Quote Link to comment Share on other sites More sharing options...
Sarahpengie Posted February 17, 2013 Author Share Posted February 17, 2013 Hi, Sorry Christian. Thanks Paul, I replaced my code with your code however I'm still having the same problem. It is still ignoring the endtime and allowing me to book 12.00-13.00 when 11.30-12.30 is booked :/ Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted February 17, 2013 Share Posted February 17, 2013 What is your table structure? Post it here, so I can have a play about. Quote Link to comment Share on other sites More sharing options...
rguitar Posted February 17, 2013 Share Posted February 17, 2013 Your 12-1 timeslot does not satisfy the condition StartTime >= (12) as compared to the 11:30 in your post. The query is asking for both conditions to be satisfied with the AND. It does with 11-12 but not 12 -1. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 17, 2013 Share Posted February 17, 2013 in my browser there stands in my browser there stands eddit in the bottom right of my posting. when i eddid my posts i dont know what prefrences you have on your accaunt..... Are those haikus? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.