Avoid booking overlap situation in PHP
P粉517090748
P粉517090748 2023-09-04 10:10:22
0
1
455
<p>I have an application that allows users to book entertainment facilities. Users can select a start date (05/05/2022) and an end date (05/08/2022) and then send the data through the database. </p> <p><em>But</em>, <strong>(Question)</strong>I have a user who wants to book the same facility. They select a start date (05/03/2022) and an end date (05/06/2022). My goal is to prevent users from booking facilities with an end date between existing booking dates. </p> <p><strong>(Implemented)</strong>I can achieve this by disabling the required amenities for the reservation if the start and end dates fall between existing reservation dates. I've completed the SQL query: </p> <pre class="brush:php;toolbar:false;">"SELECT * FROM reservations WHERE facility = :facility AND :begDate BETWEEN begDate AND endDate AND :endDate BETWEEN begDate AND endDate";</pre> <p>What are some suggestions for handling <strong>ISSUE</strong> in queries while handling <strong>ACHIEVED</strong>? </p>
P粉517090748
P粉517090748

reply all(1)
P粉554842091

You can check if a new booking overlaps an existing booking using the following logic:

SELECT * 
FROM reservations 
WHERE facility = :facility AND :begDate <= endDate AND :endDate >= begDate

Given a facility and a date range, the query will check if a reservation for the same reservation already exists with an overlapping time range. It returns the "problematic" reservation, or no rows if no conflict exists.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!