0

Say I was to offer an item to multiple users, up to a maximum of say 10 purchases can be made by any user until the item is 'deactivated'. The items are taken from the mainitems table and filtered to the specific users.

For each purchase a count is incremented in the items row to indicate the amount of purchases made. How can I control say 11 users all trying to purchase this item @ once? Is the best way to acheive this by simply locking that row until the purchase is completed and then compared? Or is there a way I can do this without having to constantly remove the results from other users and then reactivating them?

Jimmy
  • 91
  • 5

1 Answers1

1

You should use a sql transaction and in php you should use try{} catch(){} statement

try
{
//mysql begin transaction


//the code that does what you said it would do


//mysql commit transaction
}
catch(Exception $ex)
{
//mysql rollback

/return false
}

In this way, I dont think it would be the case that two users ask for the item in the same time the transactions are treated FIFO.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
iMoldovean.com
  • 141
  • 1
  • 1
  • 6