php - PDOException' 消息为 'There is no active transaction'

标签 php mysql pdo

我收到以下错误,并且无法纠正它

Fatal error: Uncaught exception 'PDOException' with message 'There is no active transaction' in /home1/jkendall/public_html/princeave.com/bo/before_care_finalize_billing.php:60 Stack trace: #0 /home1/jkendall/public_html/princeave.com/bo/before_care_finalize_billing.php(60): PDO->rollBack() #1 {main} thrown in /home1/jkendall/public_html/princeave.com/bo/before_care_finalize_billing.php on line 60

导致此问题的代码如下。我已删除插入语句并输入 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'));
}

最佳答案

您的代码永远不会启动事务。为了回滚,你需要首先创建一个事务,否则它只是插入它。下面是我添加了 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() 作为调试工作的一部分包含在内,并且希望在其中添加一些对此目的稍微有帮助的内容。

关于php - PDOException' 消息为 'There is no active transaction',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062218/

相关文章:

PHP OOP 避免在方法中重复类似的查询

php - DIV 容器保持空白

php - 适用于 Mac 的独立 Web 服务器(PHP、MySQL)

php - 如何将 SQL-View 与 PQGrid 结合使用并保存到数据库

php - 为什么我的 PHP PDO 选择在不使用页面限制的情况下?选择中的数组也有问题

php - "use"命令因 PostgreSQL 中的语法错误而失败(从 MySQL 转换)

php - 未知的 PHP artisan 修补程序输出

php - 使用标签执行图像搜索

php - fpdf 多单元格中的行高自动调整 ()

php - PDO 调用在 PHP 中失败,但在 MySQL Workbench 中成功执行