mysqli多查询无法唤醒

时间:2018-12-11 15:51:31

标签: php mysql

嗨,我有一部分代码:

foreach($cartItems as $item){
    $sql .= "INSERT INTO order_items (order_id, product_id, quantity) VALUES ('".$orderID."', '".$item['id']."', '".$item['qty']."');";

    $newstock = $item['stock']-$item['qty'];
    $sql1 .= "UPDATE product SET stock='".$newstock."' WHERE ID='".$item['id']."';";
}
// insert order items into database
$insertOrderItems = $conn->multi_query($sql);
$updateProductQty = $conn->multi_query($sql1);

并且我在更新表“ product”时遇到问题。.第一个查询为1,并且可以,但是sql1输出正常,但是什么也没有发生,我尝试在phpmyadmin中手动更新此查询,并且它在那里工作。你看到我没看到的东西吗?谢谢

编辑:看起来应该更像这样吗?

        $conn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);

             foreach($cartItems as $item) {


                    $newstock = $item['stock']-$item['qty'];
                    $stmt = $conn->prepare('update product set stock = ? where ID = ?');
                    $stmt->bind_param('ii',$newstock,$item['id']);
                    $stmt->execute();
            }
$conn->commit();
    $conn->close();

这个?

0 个答案:

没有答案