php mysql innodb使用事务多次插入不同的表失败

1l5u6lss  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(469)

我一直在研究事务,据我所知,我的多插入语句应该可以工作。
我用的是innodb引擎。下面的两个insert语句都可以工作并将数据正确地输入到相应的表中,但前提是我在事务中一次有一个insert语句,而不是两个insert语句同时使用。
有人能告诉我需要做些什么吗?
它失败了 Affected rows (INSERT): -1 代码:

$con->begin_transaction();

$con->autocommit(FALSE);

$query = "

INSERT INTO `Order` (`PO`,`SellingManagerSalesRecordNumber`,`OrderID`,
  `AmountPaid`,`CreatedTime`,`Subtotal`,`Total`,`BuyerUserID`,
  `IsMultiLegShipping`,`Status_id`,`SellerUserID_id`)
   VALUES
   ('AF11514','11514','111809902234-1906167668001','55.98',
  '2019-01-07 17:42:46','55.98','55.98','hyccas3','false',
   (SELECT `id` FROM `Status` WHERE `Status` = 'Completed'),
   (SELECT `id` FROM `SellerUserID` WHERE `SellerUserID` = 'afiperformance'))
   ON DUPLICATE KEY UPDATE PO = VALUES(PO),
   SellingManagerSalesRecordNumber = VALUES(SellingManagerSalesRecordNumber),
   OrderID = VALUES(OrderID),
   AmountPaid = VALUES(AmountPaid),
   CreatedTime = VALUES(CreatedTime), Subtotal = VALUES(Subtotal),
   Total = VALUES(Total),
   BuyerUserID = VALUES(BuyerUserID),
   IsMultiLegShipping = VALUES(IsMultiLegShipping);

INSERT INTO `CheckoutStatus` (`PO`,`LastModifiedTime`, 
         `PaymentMethod_id`,`Status_id`)
   VALUES ('AF11514','2019-01-07 17:47:55',
  (SELECT `id` FROM `PaymentMethod` WHERE `PaymentMethod` = 'PayPal'),
   (SELECT `id` FROM `Status` WHERE `Status` = 'Complete'))
   ON DUPLICATE KEY UPDATE PO = VALUES(PO),
   LastModifiedTime = VALUES(LastModifiedTime);";

echo $query;

$con->query($query);
printf("<br><br>\n\nAffected rows (INSERT): %d ", $con->affected_rows) . "\n\n<br><br>";

if ($con->affected_rows == -1) {
    echo "<br><br>\n\n Failed " . "\n\n<br><br>";
    $con->rollback();
    $commit = '';
} else {
    $commit = $con->commit();
    echo " success " . "\n\n<br><br>";
}

/* commit transaction */
if ($commit == '') {
    print("Transaction commit failed\n");
} else {
    print("Transaction commit success\n");
}

$con->close();
yb3bgrhw

yb3bgrhw1#

mysqli_query 不支持在一个调用中进行多个查询。你要么用 mysqli_multi_query 或者将查询拆分为两个单独的调用 mysqli_query . 因为您使用的是事务,所以最好分成两个调用,因为在中很难获得第二个和后续查询的错误信息 mysqli_multi_query .

相关问题