Skip to content
Advertisement

Reset value in database after ‘x’ amount of time [closed]

I am creating a competition page and have a ticket table in mysql. Each ticket contains an availability column which is either ‘1’ or ‘0’ (true or false).

When the user adds a ticket to their basket I am setting the value to 0 so that other users cant attempt to buy that ticket while it is being purchased by someone else. I am wanting a timeout so that the value will essentially set back to 1 after x amount of time (let’s say 10 minutes)

I get how to do all aspects except this timer. I have seen something about events but I am not sure how to use them.

Pseudocode for what I want

adding to basket
availability = 0

trigger timer event
after 10 mins

if purchased = 0
    availability = 1

(I am mainly controlling the site with PHP)

Advertisement

Answer

You’ll probably find life easier if rethink your strategy. You will find that if you change your availability field to be a date you can change your strategy to this, and get the same net behaviour without the added complexity of the scheduled process to unlock;

  • Allow adding to basket if availability before now and not purchased.
  • Set availability to a date in the future that the ticket becomes available to purchase and purchased = 0.
  • When someone adds the ticket to their basket set the availability field to now + 10 mins (this will ensure that for the next 10 mins that ticket cannot be claimed but after 10 mins it will automatically be available without any extra processing).
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement