<?php

include("dbconnect.php");

session_start();
$success = false;
if (isset($_COOKIE['srno'])) {
    $success = true;
    $qty = $_REQUEST['qty'];
    $code = $_REQUEST['code'];
    $size = $_REQUEST['size'];

    $gprod = mysql_query("SELECT * FROM `product` where code='$code' ") or die(mysql_error());
    $fprod = mysql_fetch_object($gprod);

    $name = $fprod->dname;

    if ($fprod->cost2 == '') {
        $cost = $fprod->cost;
    } else {
        $cost = $fprod->cost2;
    }
    $guser = mysql_query("SELECT * FROM `site_passmain` where srno='$_COOKIE[srno]' ") or die(mysql_error());
    $fuser = mysql_fetch_object($guser);

    $userid = $fuser->srno;

    mysql_query("LOCK TABLES `cart`");
    $gen1 = mysql_query("SELECT MAX(srl) AS maxcode FROM `cart` where srl != ''  LIMIT 0,1 ") or die(mysql_error());
    if (mysql_num_rows($gen1) > 0) {
        $vvouch = mysql_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else {
        $qvouch = '1';
    }
    $amount = $qty * $cost;
    mysql_query("INSERT INTO `cart`(`srl`,`user`,`qty`,`code`,`name`,`cost`,`size`,`amount`)
                            VALUES ('$qvouch','$userid','$qty', '$code',N'$name', '$cost','$size','$amount')");
    mysql_query("UNLOCK TABLES");
}
$rows = array('success' => $success);
header("Content-type: application\json");
echo json_encode($rows);
