<?php
ob_start();
session_start();
 
require_once("config.php");
$session_id = session_id();

$sqlCart = "SELECT * FROM `og_temp_cart` WHERE `product_id` = '".$_REQUEST['hid_product_id']."' AND `unique_id` = '".$session_id."'";
$exeCart = mysqli_query($connection,$sqlCart);
$numCart = mysqli_num_rows($exeCart);
if($numCart>0)
{
	$arr_cart = mysqli_fetch_array($exeCart);
	$new_quantity = $arr_cart['quantity']+1;
	$new_subtotal = $arr_cart['unit_price']*$new_quantity;
	
	$sql_update = "UPDATE `og_temp_cart` SET `user_id` = '".$_SESSION['USER_ID']."',`quantity` = '".$new_quantity."',`subtotal` = '".$new_subtotal."' WHERE `product_id` = '".$_GET['hid_product_id']."' AND `unique_id` = '".$session_id."'";
	$exe_update = mysqli_query($connection,$sql_update);
}
else
{
	$fetch_product = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `og_products` WHERE `product_id` = '".$_REQUEST['hid_product_id']."'"));

	$sql_insert = "INSERT INTO `og_temp_cart` SET `user_id` = '".$_SESSION['USER_ID']."',`product_id` = '".$_REQUEST['hid_product_id']."',`unit_price` = '".$fetch_product['price']."',`quantity` = '1',`subtotal` = '".$fetch_product['price']."',`unique_id` = '".$session_id."',`is_adult` = '".$fetch_product['is_adult']."'";
	$exe_insert = mysqli_query($connection,$sql_insert);
}

if($_GET['action'] == "cart")
{
    echo '<script>window.location.href="view-cart.php";</script>';
}
else
{
    echo '<script>window.location.href="checkout.php";</script>';
}
?>