hgkhkhk Posted November 5, 2017 Share Posted November 5, 2017 I have tried everything to prevent this scenario from happening, but everything has failed. I would be very grateful if anyone could help with this scenario. If someone has booked an equipment on a certain day between 00:00 and 06.00, then someone tries to book the same equipment on the same day between 01:00 and 05:00, how do I stop this from happening? No matter what I do, the system still inserts a record into the database.Here is the code I am using: <?php if(isset($_GET['add'])){ $equipment=$_POST['equipment']; $start=$_POST['start']; $end=$_POST['end']; $notes=mysqli_real_escape_string($dbconnection, $_POST['notes']); $eventdate=$month."/".$day."/".$year; //This code inserts the record $sqlinsert="insert into bookings (equipment,start_time,end_time,notes,selected_date,date_added) values ('".$equipment."','".$start."','".$end."','".$notes."','".$eventdate."',now())"; $resultinsert=mysqli_query($dbconnection,$sqlinsert); if($resultinsert){ echo "<span class='go'> Booking was successful </span>"; } } ?> I have tried everything: from asking the database to check for records: "SELECT * FROM bookings WHERE selected_date BETWEEN start_time AND end_time"; to using conditional statements: if($start>$start && $end<$end) { echo "no"; } But nothing is working. Thank you to anyone who can help. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 5, 2017 Share Posted November 5, 2017 Your logic is very much incorrect. I guess bookings can't span multiple days? Look for bookings on the same day where the start time is before the requested end time and the end time is after the requested start time. Quote Link to comment Share on other sites More sharing options...
hgkhkhk Posted November 5, 2017 Author Share Posted November 5, 2017 @requinix I tried doing the following, but what ended up happening is that no bookings were allowed to be made. $sql="SELECT * FROM bookings WHERE selected_date AND start_time < end_time AND end_time>start_time"; $result= mysqli_query($dbconnection, $sql); $results=mysqli_num_rows($result); if($results>0) { echo "<span class='no'> Error </span>"; }else{ //This code inserts the record $sqlinsert="insert into bookings (equipment,start_time,end_time,notes,selected_date,date_added) values ('".$equipment."','".$start."','".$end."','".$notes."','".$eventdate."',now())"; $resultinsert=mysqli_query($dbconnection,$sqlinsert); if($resultinsert){ echo "<span class='go'> Booking was successful </span>"; } What I am building is a calendar. Yes, bookings are not allowed to span multiple days. The user clicks a date and then has to select an equipment from a drop down list along with the booking times (also from drop down lists- start time and end time have their own drop down lists.) So if the user has selected 12.00 to 18.00 and then another user tries booking the same equipment on the same day from 14.00 to 15.00 (obvious time clash) the system unfortunately still goes ahead and books the second user's request. I just don't know what to do to stop this from happening. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 5, 2017 Share Posted November 5, 2017 You can stop this from happening by understanding what you're doing and thus realizing you're still wrong about it. WHERE selected_dateExplain to me what, precisely, that means. Not what you want it to mean but what it actually means. Quote Link to comment Share on other sites More sharing options...
hgkhkhk Posted November 5, 2017 Author Share Posted November 5, 2017 (edited) @requinix- selected_date is the field in the database- used to store the date which the user has selected from the calendar. I used the following variable to be store the date. $eventdate=$month."/".$day."/".$year; The $eventdate variable is then inserted into the selected_date field in the bookings table in the database. Edited November 5, 2017 by hgkhkhk Quote Link to comment Share on other sites More sharing options...
hgkhkhk Posted November 5, 2017 Author Share Posted November 5, 2017 Hi. I have managed to solve the problem. Thanks to everyone for helping. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2017 Share Posted November 5, 2017 For the benefit of those searching the forums for similar problems to theirs, it is considerably more helpful if you post the solution. 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.