<?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,餘額不足!!"; } ?>
業務邏輯php
下單以後用戶要進行判斷庫存,和用戶金額是否充足。html
充足的話進行修改數據庫的庫存和餘額。sql