dongxi1943 2012-02-20 18:49
浏览 106
已采纳

如果同时有多个进程,我如何处理mysql插入?

I'm trying to make a simple shopping cart. Normally if product quantity is 0 buy now button will disappear and showing out of stock text.

Let's say current product have 1 quantity left and at the same time have two buyer buy the product. Both of buyers already in checkout page which script is not calculate available quantity any more.

In this case, how do I validate this part? I want the process basis on first-served, who is first insert into MySQL will get the product if not, script will return back to product page.

Let me know..

  • 写回答

4条回答 默认 最新

  • dongxiezhuo8852 2012-02-20 18:52
    关注

    The simples, and thus probably best solution - Use the same script to check if the item is out of stock on the insert page. If the item is out of stock, display the error message. If it it's not out of stock, let them buy it.

    You just need to add that same select again.

    Alternatively, depending on your schema, when you update the quantity after the sale, add a where clause to the update query: WHERE the qty >= the quantity purchased. Then, see how many rows were affected. If the number of rows affected is 0, then you know the sale didn't go through. Show the out of stock error message.

    Alternatively, probably an undesirable soluation - lock out the page. When the first user goes to the page, you insert a row into a table that keeps track of who is on what pages, and prevents other uses from going to that page. Then, when the second user goes to the page, they get an error message. Then, when the first user completes the transaction, you make sure that the same session id as who locked the page is who is completing the transaction now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗