<?php header("Content-type: text/html; charset=utf-8"); session_start(); include_once("DB.class.php"); //测试数据,初始化金额为1000. // $_SESSION['account'] = 1000; // 获得订单总额 $order_money = $_GET['total']; //获得账户余额 $uid = $_SESSION['uid']; // var_dump($uid); $sql = "select account from tb_user where id = $uid"; $account = $dao->getOne($sql); if($account>=$order_money){ $order = $_SESSION['order'] ; // 从数据库中减掉库存 foreach($order as $item){ $kucun = $item['num']; $id = $item['goods_id']; $sql = "update tb_goods set number =number-{$kucun} where id ={$id}"; // var_dump($sql); // die(); $dao->exec($sql); } //减去金额 $account-=$order_money; $sql = "update tb_user set account = {$account} where id = $uid"; $dao->exec($sql); echo "下单成功!账户余额:¥$account"; $_SESSION['order'] = ''; }else{ echo "下单失败!订单所需金额:$order_money,账户余额:¥$account,余额不足!!"; } ?>
注:例子中是使用的工具类代码DB.class.php要在https://www.cnblogs.com/mzzone/p/10911891.html
业务逻辑
下单之后用户要进行判断库存,和用户金额是否充足。
充足的话进行修改数据库的库存和余额。