【发布时间】:2017-01-22 02:03:21
【问题描述】:
当我将它粘贴到 MySql 控制台时
START TRANSACTION;
INSERT INTO `orders` (customer_id) VALUES ('2');
SET @lastid=LAST_INSERT_ID();
INSERT INTO `transactions`
(order_id,product_id,product_quantity,price,ammount,customer_id)
VALUES (@lastid,'3','2','4','4','2');
INSERT INTO `transactions`
(order_id,product_id,product_quantity,price,ammount,customer_id)
VALUES (@lastid,'1','3','5','4','2');
COMMIT;
它工作正常,当我尝试通过 php 做同样的事情时
$sql = "START TRANSACTION;";
$sql .="INSERT INTO `orders` (customer_id) VALUES ('$customer_id_form');";
$sql .="SET @lastid=LAST_INSERT_ID();";
foreach ($product_id_form as $key => $product){
$sql .= "INSERT INTO `transactions`
(order_id,product_id,product_quantity,price,ammount,customer_id)
VALUES
(@lastid,'$product','$quantity_form[$key]',
'$price_form[$key]','$amount_form[$key]','$customer_id_form');";
}
$sql .= "COMMIT;";
//$sql = "INSERT INTO products (`product_name`,`curent_price`,`product_quota`) VALUES ('$productname_form','$productprice_form','$productquote_form')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
header("Location: order.php");
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
mysqli_close($con);
它不工作错误显示是
您的 SQL 语法有错误;检查手册 对应于您的 MariaDB 服务器版本,以便使用正确的语法 'INSERT INTO
orders(customer_id) VALUES ('2');SET 附近 @lastid=LAST_INSERT_ID();INS' 在第 1 行
【问题讨论】:
标签: php mysql transactions mariadb