<?php 

session_start();

$path_to_root = "";
include $path_to_root . 'database.php';

$db = new database();

if (isset($_GET['produk_id'])) {
	$id_barang = $_GET['produk_id'];
	$quantity = strip_tags(trim($_POST['quantity']));
	$id_user = $_SESSION['user_id'];

	echo 'id_barang = '.$id_barang.' dan quantity = '.$quantity.' User id = '.$_SESSION['user_id'];

	$sql = "SELECT * FROM keranjang WHERE id_barang=$id_barang AND id_user=$id_user";

    $result = $db->db_query($sql);
    $num_rows = $db->db_num_rows($result);

    if ($num_rows == 0) {
        $sql = "INSERT INTO `keranjang` (`id_user`, `id_barang`, `quantity`, `lama_sewa`) VALUES ('" . $id_user ."', '" . $id_barang . "', '" . $quantity . "', '1')";

        $result = $db->db_query($sql);

        if ($result) {
            header("location:index.php?result=success");
        } else {
            header("location:index.php?result=failure");
        }
    } else {
        header("location:index.php?result=already_available");
    }


} else {
	header("location:index.php?result=failure");
}

?>