这是我的查询,只需根据需要编辑值
$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";
如果要清理数据,则需要将 startdate 和 enddate 值替换为“?” 并做这样的事情->
$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));
更进一步:
$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 ) );
此方法不允许日期进入字段周围字段之外的字段或该区域的任何其他可能性(见图):
|| |-----------| || first booking
|-----| ok
|---| fails
|----| fails
|----| fails
如果您需要完整的解释,请转到此主题
当您执行错误消息时,您还需要执行 if else 所以这个语法应该可以完成这项工作:
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!");
}
请忽略带有 center id 和tennant id 的字段(除非您使用这些),它们在那里确保预订设置到正确的租户和正确的会议室。根据需要使用它并替换你需要的东西。
顺便说一句,我正在使用 p4a 应用程序框架,因此您需要替换P4A::singleton()->messageError
为 php 错误消息 mobober