/*
 	Insert Size & Quantity into SQ table
 */
 //create a size array from Size stack
 //(Creatd on line 97)
 //create a quantity arra from quantity stack
 $quantityArray = explode(",", $quantityStack);
 $quantity = remove_last_empty_item($quantityArray);
 //insert
 foreach ($size as $key => $s) {
     /*
     	check product with that is size already in the db or not
     	if yes fetch old size quantity from the db
     	and add new size quantity to it and update into db
     */
     if (!check_size_is_unique($s, $userId, $productId)) {
         //fetch the quantity from db
         $stmt = Db::query("SELECT quantity FROM `sq` WHERE size = ? AND user_id = ? AND product_id = ?", array($s, $userId, $productId));
         $r = $stmt->fetchAll(PDO::FETCH_ASSOC);
         $oldQuantity = $r[0]["quantity"];
         //create new quantity
         $newQuantity = $oldQuantity + $quantity[$key];
         //update new quantity into database
         Db::update("sq", array("quantity" => $newQuantity), array("size", "=", $s, " AND ", "user_id", "=", $userId, " AND ", "product_id", "=", $productId));
     } else {
         Db::insert("sq", array("size" => $s, "quantity" => $quantity[$key], "user_id" => $userId, "product_id" => $productId));
     }
 }
 //show success message
 $result["message"] = "Product added";
 $result["return"] = true;
 /*
 	Insert new Size & Quantity into SQ table
 */
 //create a size array from Size stack
 //(Creatd on line 97)
 //create a quantity arra from quantity stack
 $quantityArray = explode(",", $quantityStack);
 $quantity = remove_last_empty_item($quantityArray);
 //insert
 foreach ($size as $key => $s) {
     /*
     	check product with that is size already in the db or not
     	if yes fetch old size quantity from the db
     	and add new size quantity to it and update into db
     */
     if (!check_size_is_unique($s, $userId, $pId)) {
         //fetch the quantity from db
         $stmt = Db::query("SELECT quantity FROM `sq` WHERE size = ? AND user_id = ? AND product_id = ?", array($s, $userId, $pId));
         $r = $stmt->fetchAll(PDO::FETCH_ASSOC);
         $oldQuantity = $r[0]["quantity"];
         //create new quantity
         $newQuantity = $oldQuantity + $quantity[$key];
         //update new quantity into database
         Db::update("sq", array("quantity" => $newQuantity), array("size", "=", $s, " AND ", "user_id", "=", $userId, " AND ", "product_id", "=", $pId));
     } else {
         Db::insert("sq", array("size" => $s, "quantity" => $quantity[$key], "user_id" => $userId, "product_id" => $pId));
     }
 }
 //show success message
 $result["message"] = "Product updated succesfully";
 $result["return"] = true;