Php-mysql How to realize the money-shopping process (details about a lot of questions)

Source: Internet
Author: User
Php-mysql How to achieve buy things to deduct money flow (a lot of details)?
Assuming that the user spends money on the balance of 1 ' sugar ' This virtual commodity, the money balance and the number of users have ' sugar ' recorded on different tables, then we have to do is to deduct the user's money at the same time, the user's virtual inventory of the number of ' sugar ' +1. Suppose any user ' sugar ' is capped at 99

First, the current younger brother's ideas:

* Select to check the money balance on the user account (SQL statement 1)
* Compare money balances and a ' sugar ' price if the balance is sufficient then continue (PHP logic implementation)
* Transaction Start
*update the money balance and subtract 1 ' sugar ' from the price. Here's the question a: is the balance after the direct update to the ' sugar ' price (that is, PHP calculates the balance first), or does the MySQL do field subtraction? (SQL statement 2)
*select user ' sugar ' number (SQL statement 3)
* If the previous select has no results, then insert a record of the user's ' sugar ', the number of the purchase of ' sugar ', if the user's ' sugar ' record, then update ' sugar ' number, so that it plus 1. There is a problem here B: Because the maximum number of ' sugars ' for a single user is 99, I need to use PHP logic to determine if the ' sugar ' number +1 exceeds the upper limit of 99, but if there is no ' for update ' lock table in SQL statements 1 and 3, then combine issue a, once the user sends 2 buy ' Sugar ' request, it may appear that actually the number of ' sugars ' exceeds the upper limit of 99 (for example, reaching 100)
* Final Transaction submission

So there are 2 final questions:
C: Similar to the logic that consumes a resource, in exchange for another resource, must be ' select...for Update ' lock when a resource is select? (no first select and cannot make money enough or ' sugar ' whether the amount of the logical judgment, select after update and afraid of the Select to the data in the middle of the change)
D: Is there a way to simplify the above process?

Or there is no exhaustive article on this issue, if you can share it with gratitude.
------to solve the idea----------------------
This thing is guaranteed to be foolproof, it has to use the transaction, or the failure to rollback, the successful submission
------to solve the idea----------------------
Transaction processing must be used.
------to solve the idea----------------------
The transaction is first set, then the select for update takes out the current inventory, inserts a record into the log table if enough, and then update the inventory complete deduction, and finally releases the transaction.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.