This is my query, just edit the values as you need it
$sql = "SELECT COUNT(*) as num FROM meeting_room_bookings
WHERE
(
(startdate < start_at AND startdate > start_at)
OR
(startdate > start_at AND enddate < end_at)
)
AND
meeting_room_id = whatever_id i was on about at the time :P";
if you are going to sanitise the data then you will need to replace the startdate and enddate values with "?" and do something like this->
$sql = "SELECT COUNT(*) as num FROM meeting_room_bookings
WHERE
(
(? < start_at AND ? > start_at)
OR
(? > start_at AND ? < end_at)
)
AND
meeting_room_id = ?";
$result = p4a_db::singleton()->fetchRow($sql, array($date, $date, $date, $dateend, $merono));
to go one further:
$sql = "SELECT COUNT(*) as num FROM meeting_room_bookings
WHERE
(
( start_at >= ? AND start_at <= ? )
OR
( start_at <= ? AND end_at >= ? )
OR
( end_at >= ? AND end_at <= ? )
OR
( ? >= ? )
)
AND
meeting_room_id = ?";
$result = p4a_db::singleton()->fetchRow( $sql, array( $date, $dateend, $date, $dateend, $date, $dateend, $date, $dateend, $merono ) );
this method allows no dates to come within the fields outside the fields around the fields or any other possibility from that area (see diagram):
|| |-----------| || first booking
|-----| ok
|---| fails
|----| fails
|----| fails
if you need a full explanation go to This Topic
when you do the error messages you will need to also do if else so this syntax should do the job:
if ( 0 == $result["num"] )
{
p4a_db::singleton()->query( "INSERT INTO meeting_room_bookings ( start_at, end_at, meeting_room_id, tennant_id, centre_id )
VALUES
( ?, ?, ?, ?, ? )", array( $date, $dateend, $merono, $row['tennant_id'], $centreRow['id'] ) );
return true;
}
else
{
P4A::singleton()->messageError("The selected times conflict with another booking. Please select another time or meeting room!");
}
please ignore the fields with the centre id and tennant id (unless you use these) they are there to ensure the booking is set to the right tennant and the right meeting room. use this as required and substitute what you need to.
btw i am using p4a application framework so you will need to replace P4A::singleton()->messageError
with the php error message mobober