I’m designing a car booking system and I’m trying to write a query that will check whether a car is available. What I want the query to do in english is to check that the start hire and end hire selected which are stored in variables $mysql_startdate and $mysql_enddate do not overlap with any existing bookings.
I have checked that the variables in the query have the value that they should have so I’m guessing there is a problem with my syntax, most likely the last part.
Format of datetime variables:
$mysql_startdate = 2012-03-22 00:00
$mysql_enddate = 2012-03-23 00:00
I am getting no errors from mysql_error, the problem is that no results are displayed even if a car that is available is requested.
$query = "SELECT c.*,b.startHire,b.endHire FROM cars c JOIN bookings b WHERE c.currentBranch = '$pickUpLocation' AND NOT (b.startHire > '$mysql_enddate' OR b.endHire < '$mysql_startdate')";
The startHire and endHire fields in the database are set to datetime and have the format – 2012-03-02 00:00:00 etc.
Can you help me to identify the problem and amend the sql query?
Let me know if you need any further code/information.
Thank you!
Try that: