1. $num = 1;
  2. $good_id = 199758;
  3. $model = Db::name("tettt");
  4. $model->execute("set autocommit=0");
  5. $model->startTrans();
  6. //SELECT data AS old_data, version AS old_version FROM …
  7. $data = Db::name("tettt")->lock(true)->where('id',$good_id)->find();
  8.  
  9. if(empty($data )){
  10.     return "商品不存在";
  11. }
  12. $total = $data['count'];  //商品库存
  13. if($total< $num){
  14.     return "库存不足";
  15. }
  16. $data['count'] = $total-$num;
  17. $res = $model->where('id',$good_id)->update($data);
  18. if($res){
  19.     $model->commit();
  20.     return "成功购买".$num."个";
  21. }else{
  22.     $model->rollback();
  23.     return "shibai";
  24. }


相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!