<?php
/**
* Created by PhpStorm.
* User: song tong jing
* Date: 2018/11/5
* Time: 15:30
*/
header("content-type:text/html;charset=utf-8");
try{
$pdo = new PDO("mysql:host=127.0.0.1;dbname=student","root","root");
$pdo->exec("set names utf8");
}catch (PDOException $exc){
echo $exc->getTraceAsString();//获取字符串的错误信息
}
//事务回滚
$pdo->beginTransaction();//开启事务
try{
$price_sql="select price from goods where g_id=1";//搜索出来价格
$res=$pdo->query($price_sql)->fetch();
$price=intval($res['price']);
$sql1="update member set money=money-$price where men_id=1";
$result = $pdo->exec($sql1);
if(!$result)
{
throw new Exception("张三钱包减钱失败");
}
$sql2="update members set money=money+$price where men_id=1";
$result=$pdo->exec($sql2);
if(!$result)
{
throw new Exception("李四钱包加钱失败");
}
$sql3="update exam_goods set number=number-1 where g_id=1";//修改库存
$result=$pdo->exec($sql3);
if(!$result)
{
throw new Exception("库存减1失败");
}
$pdo->commit();//事物自动提交
}catch(Exception $ex){
$pdo->rollBack();//回滚
echo $ex->getMessage();//得到错误信息
}