Skip to content
Advertisement

Limiting number of reservations per user with PHP and MySQL

My users are able to create a reservation, but I would like to limit it – let’s say one user can make max 5 reservations with “Pending” status, after Admin approves reservation and pending status changes to approve, the user can make another reservation.

Pending status is given by default when the user makes the reservation. What I would like to achieve is, that if the reservations table has 5 Pending reservations under the same user_id, the user cannot make a new reservation, so he gets an alert that he can not make more reservations.

My question is, how can I achieve this? Is it by using COUNT, or should I create a trigger for it? Would be grateful for any advice about how to handle this.

Advertisement

Answer

The easiest way is to execute another query when handling user input. The new query would execute something similar to SELECT COUNT(*) FROM reservations WHERE user_id=? AND status='pending'. You can then check in PHP the result of this query. If it is greater than or equal to the limit you imposed then display a message to the user informing them that they reached max pending reservations and they need to wait.

Pseudo-code:

  1. If new reservations is requested

  2. Prepare a statement to check for the number of pending reservations for the current user.

  3. Execute and fetch the result

  4. If the count >= 5

    4.1. Stop processing the request and display a message to the user

  5. Else

    5.1. Proceed with the insertion of the new reservation.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement