D
D
del9937882016-11-24 21:04:05
PHP
del993788, 2016-11-24 21:04:05

How to automatically delete a record from the database after 10 minutes?

Hello. Faced a problem. I am currently building a website for booking hotel rooms. In theory, the script should work like this:

  1. A person visits the site
  2. The script checks the available rooms and dates and shows them to the person
  3. A person selects the dates of the reservation and the number and presses to pay
  4. The script checks the availability of these numbers
  5. If the rooms are still available, the script writes a record to the database with booking dates, room, customer details, etc.
  6. A person goes to the booking payment page (to the Yandex checkout, for example)

Now 3 scenarios
  1. The person paid for the room and was transferred back to the site with congratulations on the booking
  2. The person closed the payment page (did not pay)
  3. The person was inactive for 10 minutes (did not pay)

I probably need to clarify the 5th point. This is done so that no one else can book a room for these dates while our person pays for it.
Well, the problem is how to delete an entry from the database if the person has not paid for the reservation?
cron is not an option. If you set the script to run every 10 minutes, then there is a chance that a person will reserve rooms at the 8th or 9th minute. That is, while the person was paying for them, the records were deleted from the database, which is not good.
I generally imagine it like this: at the 5th point, another line is written to the database, like: paid? Not really. And after the 5th point has passed, after 10 minutes the script should work, which will check the records in the database for the presence of "no" in the "paid" column, and delete the entire line, if any.
How to implement this? Tell me where to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-11-24
@del993788

Option 1 - set booking time, run cron once per minute, delete all unconfirmed bookings older than 10 minutes.
Option 2 - run the MySQL scheduler and, along with the booking, create an event to delete it if there is no confirmation.

V
vjjvr, 2016-11-24
@vjjvr

When writing to the database, mark the end time of the reservation.
In your business logic, treat all expired records as invalid.
You can clear expired ones at least once an hour, at least once a day, at least once a month - it will no longer matter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question