欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php 事务回滚

程序员文章站 2024-03-14 13:50:22
...
thinkphp5
        
try {
            $user_add['user_id'] = $post_data['openid'];
            //开启事务
            Db::startTrans();
            $re['add1'] = Db::table($prefix . 'tf_goods_user')->insert($user_add);

            //组装数据 往表goods_order 记录订单信息
            $order_add['order_no'] = $order_no;
            $re['add2'] = Db::table($prefix . 'tf_goods_order')->insert($order_add);
 
            if (in_array('0', $re)) {
                throw new \Exception('数据实例化失败');
            }
            //提交事务            Db::commit();
           
        } catch (Exception $e) {
            //如获取到异常信息,对所有表的删、改、写操作,都会回滚至操作前的状态:
            Db::rollback();
            $return['status'] = 0;
            $return['info'] = '提交失败,请重新操作';

        }
相关标签: php整理