<?php

ob_start();

if (!session_start()) {
    session_start();
}

if ($_REQUEST['insert_id']) {
    $_SESSION['bookid'] = $_REQUEST['insert_id'];
}

if ($_SESSION['CustId'] > 0) {
    try {
        $db = new PDO('mysql:host=localhost;dbname=gdhhsxmy_monsoonvibespublica;charset=utf8', 'gdhhsxmy_monsoonvibespublica', 'monsoonvibespublica');
    } catch (Exception $e) {
        echo 'Error has Occured';
    }
    $insert_id = $_REQUEST['insert_id'];
    $qun       = 1;
    $Cid       = $_SESSION['CustId'];



    $query = $db->prepare("select Cid , Pid from cart where Cid=$Cid AND Pid=$insert_id ");
    $query->execute();

    if ($query->rowCount() > 0) {
        $queryUpdate = $db->prepare("update cart SET Quantity = Quantity + $qun where Cid=$Cid AND Pid=$insert_id ");
        $queryUpdate->execute();
    } else {
        $stmt = $db ->prepare("insert into cart values(null, $Cid , $insert_id , $qun)");
        $stmt -> execute();
    }
} else {
    try {
        $db = new PDO('mysql:host=localhost;dbname=gdhhsxmy_monsoonvibespublica;charset=utf8', 'gdhhsxmy_monsoonvibespublica', 'monsoonvibespublica');
    } catch (Exception $e) {
        echo 'Error has Occured';
    }
    $insert_id = $_REQUEST['insert_id'];
    $qun       = 1;
    $Cid       = rand(10000, 99999999);

    $_SESSION['TempCustId'] = $Cid;

    $query = $db->prepare("select Cid , Pid from cart where Cid=$Cid AND Pid=$insert_id ");
    $query->execute();

    if ($query->rowCount() > 0) {
        $queryUpdate = $db->prepare("update cart SET Quantity = Quantity + $qun where Cid=$Cid AND Pid=$insert_id ");
        $queryUpdate->execute();
    } else {
        $stmt = $db ->prepare("insert into cart values(null, $Cid , $insert_id , $qun)");
        $stmt -> execute();
    }
}

header('location: cart.php');
