我正在尝试将订单输入 MySQL 数据库。它在订单表中为每个订单添加一条记录,然后遍历购物篮中的所有商品。然后它选择每个项目的信息,更改该项目的承诺库存,然后将信息添加到ordered_products 表。这就是问题所在。通过 PHP 执行 SQL 时,它不会添加到 ordered_products 表中,除非 product_id 小于或等于 4。如果我从 phpMyAdmin 执行 SQL,情况并非如此。下面是PHP。
$error = false;
for ($i = 0; $i < count($basket); $i++) {
$productid = $basket[$i]['id'];
$quantity = $basket[$i]['quantity'];
$postage = $basket[$i]['postage'];
$stmt->prepare("SELECT bodywork, wheels, seat, mechanical, batteries, `range`, `keys`, charger, serial_number FROM products WHERE product_id = ?");
$stmt->bind_param('i', $productid);
$stmt->execute();
$stmt->bind_result($bodywork, $wheels, $seat, $mechanical, $batteries, $range, $keys, $charger, $serialnumber);
$stmt->fetch();
echo 'Product id = ' . $productid . '<br />';
$stmt->prepare("INSERT INTO ordered_products (order_id, product_id, quantity, postage, bodywork, wheels, seat, mechanical, batteries, `range`, `keys`, charger, serial_number) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('iiiisssssiiis', $orderid, $productid, $quantity, $postage, $bodywork, $wheels, $seat, $mechanical, $batteries, $range, $keys, $charger, $serialnumber);
$stmt->execute();
$stmt->prepare("UPDATE products SET committed_stock = committed_stock + ? WHERE product_id = ?");
$stmt->bind_param('ii', $quantity, $productid);
$stmt->execute();
if ($stmt->error) {
echo 'An error occured, please try again. error = ' . $stmt->error;
$error = true;
}
}
$stmt->close();
$db->close();
if (!$error) {
//unset($_SESSION['basket']);
echo 'Your order has been completed, thank you!';
}
我希望我在这里提供了足够的信息。因为我可以直接输入超过 4 的值,所以我排除了数据库配置错误(我希望这是正确的!)。