我收到以下错误,并且无法纠正它
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'));
}
注释:
将事务命令放在 while 循环中可确保仅当所有记录均成功提交时数据库才会更改。
正如其他人指出的那样,将
exit()
或die()
放入生产代码中通常是一个坏主意。看来您已将exit()
作为调试工作的一部分包含在内,并且希望在其中添加一些对此目的稍微有帮助的内容。
关于php - PDOException' 消息为 'There is no active transaction',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062218/