<?php
session_start();
include 'koneksi.php';

if(!isset($_SESSION['user'])){
    header("Location: index.php");
    exit;
}

$username = $_SESSION['user'];
$produk_id = $_GET['id'];

/* cek produk sudah ada di keranjang */
$cek = mysqli_query($conn, "
    SELECT * FROM keranjang
    WHERE username='$username' AND produk_id='$produk_id'
");

if(mysqli_num_rows($cek) > 0){
    mysqli_query($conn, "
        UPDATE keranjang
        SET qty = qty + 1
        WHERE username='$username' AND produk_id='$produk_id'
    ");
} else {
    mysqli_query($conn, "
        INSERT INTO keranjang(username, produk_id, qty)
        VALUES('$username','$produk_id',1)
    ");
}

/* redirect langsung ke keranjang */
header("Location: keranjang.php");
exit;
?>