MySQL transaction rollback driven by ThinkPHP database

Time:2020-1-12

1. Start transaction method starttrans()
2. Transaction commit method commit()
3. Transaction rollback method rollback ()

Usage example:

$order = M(‘order’);
$alladded = true; // first set a value to true;
$data['name'] = 'winter';
$order - > starttrans(); // start things
for($i = 1;$i<3;$i++){ 
    $sign = $order - > Add ($data); // add a piece of data to the order table
    if(!$sign){
        $order - > rollback(); // if the order fails to add, roll back
        $alladded = false; // and set alladded to false
    }
}
// rollback
if($allAdded){
    $order->commit(); 
    //If alladded is true, both data are successful; then commit
    Echo 'successfully added';
}else{
    Echo 'add failed';
}

If you commit. Then submit the insert data. If it is found that alladded is false, there are pieces of data not inserted correctly. Then rollback will cancel all the actions of operating the database after the transaction is opened.

Provide an example to help you understand

public function insertdata(){
    $model = M();
    $model->startTrans();
    $data['name'] = 'winter';
//Add a user information
    $si = $model->table(C('DB_PREFIX').'user')->add($data);
    if(!$si){
        $model->rollback();
        exit();
    }
    $car['brand'] = $si;
//Add a user ID
    $c = $model->table(C('DB_PREFIX').'car')->add($car);
    if($si && $c){
        $model->commit();
    }else{
        $model->rollback();
    }
}
Copy code

Example two:

//Withdrawal operation
    public function ajax_tx()
    {
         //Determine whether to log in
        if(session('uid') == NULL || session('uid') == "" || session('uid') == false) {
           echo "300";
            exit;
        }else{
            //Accept data submitted by withdrawal
            
            $txnumber = (($_POST['txnumber']));

            $name = ($_POST['name']);
            $alipay_number = ($_POST['alipay_number']);
            $data = M("fanxian_new")->where("f_uid='".session('uid')."'")->find();
            if(empty($txnumber)){
                Echo "301"; exit; // withdrawal Jindou cannot be empty
            }else{
                if(empty($data) || ($data['f_fan_fee'])==0){
                    Echo "302"; exit; // no withdrawal
                }else{
                    If ($data ['f_fan_fee '] starttrans(); // open things
                        //Operation withdrawal record
                        $fanxian_new_log = M("fanxian_new_log");
                        $fanxian_new_log->f_num_fee = ($txnumber,2);
                        $fanxian_new_log->f_lmId = session("uid");
                        $fanxian_new_log->f_create_time = ();
                        $fanxian_new_log->f_name = $name;
                        $fanxian_new_log->f_alipay = $alipay_number;
                        $Fanxian new log - > F status = 1; // 1 means withdraw record 0 means return record
                        $jdtx = $fanxian_new_log->add();

                        $fanxian_new = M("fanxian_new");
                        $fanxian_new->f_uid    = $data['f_uid'];
                        $fanxian_new->f_fan_fee = $data['f_fan_fee']-($txnumber,2);
                        $jd = $fanxian_new->save();
                        if(!empty($jdtx) && !empty($jd)){
                            // submit
                            $model->commit();
                            Echo "200"; exit; // withdrawal succeeded
                        }else{
                            // rollback
                            $model->rollback();
                            Echo "306"; exit; // withdraw failed
                        }
                        
                    }
                }
            }
            
        }
    }