PHP MySQL InnoDB 使用事务多次插入不同表失败
PHP MySQL InnoDB Multiple Inserts to Different Tables Using Transactions Failed
我一直在研究交易,据我所知,我的多重插入语句应该可以工作。
我正在使用 InnoDB 引擎。下面的两个插入语句都可以正常工作并正确地将数据输入到相应的表中,但前提是我在事务中一次有其中一个,而不是同时有两个。
谁能告诉我我需要做哪些不同的事情?
失败 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();
mysqli_query
不支持一次调用多个查询。您需要使用 mysqli_multi_query
或将查询拆分为对 mysqli_query
的两个单独调用。由于您正在使用事务,因此分成两次调用会更好,因为很难在 mysqli_multi_query
.
中获取第二次和后续查询的错误信息
我一直在研究交易,据我所知,我的多重插入语句应该可以工作。
我正在使用 InnoDB 引擎。下面的两个插入语句都可以正常工作并正确地将数据输入到相应的表中,但前提是我在事务中一次有其中一个,而不是同时有两个。
谁能告诉我我需要做哪些不同的事情?
失败 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();
mysqli_query
不支持一次调用多个查询。您需要使用 mysqli_multi_query
或将查询拆分为对 mysqli_query
的两个单独调用。由于您正在使用事务,因此分成两次调用会更好,因为很难在 mysqli_multi_query
.