Transferring all Cookie's data saved in one table to an actual real Shopping Cart table

Viewed 20

I have a lot of Items saved in my database under one user cookie id, and now I want to add all that users cookie's to real shopping cart when he/she login, So I tried this

$fetch_cookie_data = $conn->query("SELECT * FROM cookie_shopping_cart WHERE cookie_id = '$cookie_id'");
foreach($fetch_cookie_data as $fetched_cookie_data){
all fetched data here

But I want to check if any of the item has previously added to user's main real shopping cart, So to check that

$check_data_in_cart = $conn->query("SELECT id FROM shopping_cart_data WHERE item_id = '$item_id'");

If there is, just replace the quantity of the item with the chosen quantity in cookie for that item only and the rest insert all to the real shopping cart

  if($check_data_in_cart->num_rows > 0){
  $update_data_in_cart = $conn->query("UPDATE shopping_cart_data SET quantity = 
 '$quantity ' WHERE item_id = '$item_id'"); 
 }

Else insert all Cookie's data to real shopping cart data, and for this I tried this

}else{
 $insert_data_in_cart = $conn->query("INSERT INTO shopping_cart_data (cols...) VALUES (fetched 
 from the first under foreach loop");
  }
 } // this for foreach loop closing curly bracket 

But Unfortunately with that code I can insert and/or update one row only, What can be the main problem?

I appreciate for any help in Advance.

0 Answers
Related