Avoid booking overlap situation in PHP
P粉517090748
2023-09-04 10:10:22
<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>
You can check if a new booking overlaps an existing booking using the following logic:
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.