0

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!).

Joseph Duffy
  • 4,566
  • 9
  • 38
  • 68
  • 3
    You need to move your error if into the for loop, it's only checking if the very last item had an error. – Josh Apr 13 '12 at 19:55
  • Thanks, Josh, I didn't realise that. I have moved it, but no errors are showing and the order is still going through (I have updated the OP to include the changes) – Joseph Duffy Apr 13 '12 at 20:07
  • You are possibly leaving the DB in an inconsistent state. You should enable PDO exceptions and wrap the whole thing in a transaction, combined with a try/catch, see http://stackoverflow.com/questions/10145259/ for a short example. – DCoder Apr 13 '12 at 20:11
  • I have tried the try/catch method, but I'm afraid I'm not that knowledgeable when it comes to PDO. I have used `new mysqli('host', 'username'...`, rather than `new PDO...`, so I'm not sure how to do the PDO parts, since to my knowledge, PDO and mysqli are different in that respect. – Joseph Duffy Apr 13 '12 at 20:56

2 Answers2

0

:) As stated in the for loop when i<= basket . From there results that the basket has 4 elements :) thats why ur sql is executing when <=4 . I think your db inserts limitaton is because your for loop . Sorry if i understood wrong your question but i got lost in the first 3 lines :).

P.S. If you have no limitations with phpmyadmin and you have limitation form your dynamic insert/update (php script) it's clearly somethng with your script's logic. I don't think it has to do with any db constraints . Check your php code constraints :)

P.P.S. Check your bascket data

Cata Cata
  • 166
  • 1
  • 9
0

The actual problem here was that that keys and charger fields were not accepting null values, so I set the default to 0. I am surprised that it didn't give me this error. I guess I need to work on my skills for finding errors such as this.

Joseph Duffy
  • 4,566
  • 9
  • 38
  • 68