Results 1 to 2 of 2

Thread: A shoping Cart order

  1. #1
    Join Date
    Jan 2006
    Location
    Kafr Sakr, Egypt
    Posts
    3

    A shoping Cart order

    Hello,
    I do an application to deal with products and shoping. I use MySQL as a database and PHP as a scripting language. I have a problem with the update of products table when

    a client decide to order some products. The products table includes field named product_qty and this field is an integer. To perform the

    order, the application should update the value of product_qty to substract the amount ordered of the product from the current value of

    product_qty. The update query will be done several times equals to the number of products presented in the order list. Suppose other

    clients already ordered a certin product which is already found in the order list, where, the value of product_qty will be less than zero and

    this value is disallowed so one of the products in this client list will not be available.
    The qusetion is, How can I stop all the update queries by this user if one of them could not be done. In other word, I want the MySQL

    server to cancel all updates if one of them is could not to be done and prevent other clients to update while the current user doing an

    update. Is this what they called "Transaction"?
    Best Regards,
    Said Bakr

  2. #2
    Join Date
    Jan 2006
    Posts
    6
    Yes, on an InnoDB table you could do a transaction and ROLLBACK when a query fails. The standard MyISAM tables do not support transactions.

    Your best solution without transaction support is to do two loops, one that checks availability for all products, and another that updates the products.

    You may still have a problem if two customers order the same product in the same few milliseconds, but the risk is minimal. To eliminate all risk, you can use "LOCK TABLES tablename WRITE" before both loops, and "UNLOCK TABLES" after the loops.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •