I'm not sure if it's just a case of me not "getting" how loops work but I can't for the life of me figure this out.
I've built an very basic shopping cart system. When a user checks out, this part of the code takes what's in the users shopping cart and adds each item to an "ordered items" table along with the corresponding order number.
The loop does work apart from one thing. It loops twice! For example if there are items 1, 2, 3 in the cart, the loop will add the items to the "ordered items" table twice . . . i.e:
ordered_items_id items_id order_id
1 1 001
2 2 001
3 3 001
4 1 001
5 2 001
6 3 001
Here's the code:
$orders_items_sql = mysql_query("SELECT items_id FROM store_cart WHERE customers_id='".$_SESSION['customers_id']."'");
while($row = mysql_fetch_array($orders_items_sql)){
$items_id = $row['items_id'];
mysql_query("INSERT INTO store_orders_items (orders_items_id,
orders_id,
items_id)
VALUES ('',
'$orders_id',
'$items_id')");
}
Why is it doing this? Tried with a for loop too and the same thing happens! Any help appreciated!
Thanks