PDOException'带消息'没有活动事务'

时间:2017-07-12 15:41:48

标签: php mysql pdo

我收到以下错误,我无法更正

  

致命错误:/home1/jkendall/public_html/princeave.com/bo/before_care_finalize_billing.php:60消息'没有活动事务'的未捕获异常'PDOException'堆栈跟踪:#0 / home1 / jkendall / public_html /princeave.com/bo/before_care_finalize_billing.php(60):PDO-> rollBack()#1 {main}在第60行的/home1/jkendall/public_html/princeave.com/bo/before_care_finalize_billing.php中抛出

导致此问题的代码如下。我已经删除了insert语句并输入了一个echo语句并运行代码来验证数据是否已通过,并且确实如此。任何建议将不胜感激。

<?php
if(isset($_POST['set']) && isset($_POST['set']) !="") {
    $size = count($_POST['charge_id']);
    $i = 0; 
    while ($i < $size) {
        $usid=$_SESSION['uid'];
        $approval_date = date('Y-m-d');
        $bill_date = date('Y-m-d');
        $transaction_date = date('Y-m-d');
        $customer_ID = $_POST['customer_ID'][$i];
        $student_ID = $_POST['student_ID'][$i];
        $full_name = $_POST['full_name'][$i];
        $account = $_POST['account'][$i];
        $description = $_POST['description'][$i];
        $bill_amount = $_POST['bill_amount'][$i];
        echo $usid." Approval ".$approval_date." Bill ".$bill_date." Transaction ".$transaction_date." ".$customer_ID." ".$student_ID." ".$full_name." ".$account." ".$description." ".$bill_amount."<br> ";
        try{
            $stmta[$i] = $db->prepare("INSERT INTO bo_transactions(transaction_date, customer_ID, student_ID, full_name, account_num, description, bill_amount, bill_date, user_id, approval_date) VALUES (:transaction_date, :customer_ID, :student_ID, :full_name, :account, :description, :charge, :bill_date, :usid, :approval_date)");
            $stmta[$i]->bindParam(':usid',$usid,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':approval_date',$approval_date,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':bill_date',$bill_date,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':transaction_date',$transaction_date,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':customer_ID',$customer_ID,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':student_ID',$student_ID,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':full_name',$full_name,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':account',$account,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':description',$description,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':bill_amount',$bill_amount,PDO::PARAM_STR);
            $stmta[$i]->execute();
            //exit();
        }
            catch(PDOException $e){
                $db->rollBack();
                echo $e->getMessage();
                //exit();
            }
        ++$i;
    }
    header(sprintf('Location: before_care_select_students.php'));
}

1 个答案:

答案 0 :(得分:1)

您的代码永远不会启动交易。为了回滚,您需要先创建一个事务,否则它只是要插入它。以下是我添加了beginTransaction()commit()函数的代码。

<?php
if(isset($_POST['set']) && isset($_POST['set']) !="") {
    $size = count($_POST['charge_id']);
    $i = 0; 

    $db->beginTransaction();
    while ($i < $size) {
        $usid=$_SESSION['uid'];
        $approval_date = date('Y-m-d');
        $bill_date = date('Y-m-d');
        $transaction_date = date('Y-m-d');
        $customer_ID = $_POST['customer_ID'][$i];
        $student_ID = $_POST['student_ID'][$i];
        $full_name = $_POST['full_name'][$i];
        $account = $_POST['account'][$i];
        $description = $_POST['description'][$i];
        $bill_amount = $_POST['bill_amount'][$i];
        echo $usid." Approval ".$approval_date." Bill ".$bill_date." Transaction ".$transaction_date." ".$customer_ID." ".$student_ID." ".$full_name." ".$account." ".$description." ".$bill_amount."<br> ";

        try{
            $stmta[$i] = $db->prepare("INSERT INTO bo_transactions(transaction_date, customer_ID, student_ID, full_name, account_num, description, bill_amount, bill_date, user_id, approval_date) VALUES (:transaction_date, :customer_ID, :student_ID, :full_name, :account, :description, :charge, :bill_date, :usid, :approval_date)");
            $stmta[$i]->bindParam(':usid',$usid,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':approval_date',$approval_date,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':bill_date',$bill_date,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':transaction_date',$transaction_date,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':customer_ID',$customer_ID,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':student_ID',$student_ID,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':full_name',$full_name,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':account',$account,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':description',$description,PDO::PARAM_STR);
            $stmta[$i]->bindParam(':bill_amount',$bill_amount,PDO::PARAM_STR);
            $stmta[$i]->execute();
            //exit();
        }
            catch(PDOException $e){
                $db->rollBack();
                echo $e->getMessage();
                die("Bad insert");
            }
        ++$i;
    }

    $db->commit();
    header(sprintf('Location: before_care_select_students.php'));
}

注意:

  1. 将事务命令放在while循环周围,确保只有在成功提交所有记录后才更改数据库。

  2. 正如其他人所指出的,将exit()die()放入生产代码中通常是一个坏主意。您似乎已将exit()作为调试工作的一部分,并希望在那里添加一些对此更有帮助的内容。