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

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

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

if($aksi == "tambah"){
    mysqli_query($conn, "
        UPDATE keranjang
        SET qty = qty + 1
        WHERE username='$username' AND produk_id='$produk_id'
    ");
}

if($aksi == "kurang"){
    $cek = mysqli_query($conn, "
        SELECT qty FROM keranjang
        WHERE username='$username' AND produk_id='$produk_id'
    ");
    $data = mysqli_fetch_assoc($cek);

    if($data['qty'] > 1){
        mysqli_query($conn, "
            UPDATE keranjang
            SET qty = qty - 1
            WHERE username='$username' AND produk_id='$produk_id'
        ");
    } else {
        mysqli_query($conn, "
            DELETE FROM keranjang
            WHERE username='$username' AND produk_id='$produk_id'
        ");
    }
}

header("Location: keranjang.php");
exit;
?>