Home  >  Q&A  >  body text

Avoid booking overlap situation in PHP

<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粉517090748P粉517090748434 days ago461

reply all(1)I'll reply

  • P粉554842091

    P粉5548420912023-09-05 09:14:42

    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.

    reply
    0
  • Cancelreply