mardi 4 août 2015

SQL "Update" Query occurring happens once in PHP "Foreach" loop but loop still runs

I am trying to have a foreach loop run through an array and update my database for each entry.

My code looks like this:

$basketID = mysqli_insert_id($conn);
$basket = $_SESSION['basket'];
$x = array_count_values($basket);
foreach($x as $prodID => $Quant){
    $sql = "
    UPDATE products SET stock = (stock - '$Quant') 
    WHERE productID = '$prodID'; 

    INSERT INTO basketitems(basketID, productID, quantity)
    VALUES ('$basketID','$prodID','$Quant'); ";

    mysqli_multi_query($conn, $sql);
}

The result at the moment is that the loop occurs say 5 times (if I have 5 items in the array) but the query only happens once [I placed a counter in to find out if it was breaking the loop and it counted 5 times]. If I remove the "Update" query then 5 items are inserted into the basketitems table.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire