I'm trying to input orders into a MySQL database. It adds one record per order in an order table, then loops through all the items in the basket. It then selects information on each item, cahnges the commited stock for that item and then adds the information to an ordered_products table. This is where the problem lies. When executing the SQL through PHP it does not add to ordered_products table, unless the product_id is less than or equal to 4. This is not the case if I execute the SQL from phpMyAdmin. Below is the 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!';
}
I hope I have provided enough information here. Because I am able to input values more than 4 when doing it directly, I have rules out a database-configuration error (I hope that's correct!).