%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/www/website_6ca89795/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/www/website_6ca89795/update-cart.php
<?php
session_start();
include('dbcon.php');

if (!isset($_SESSION['session_id'])) {
    $_SESSION['session_id'] = session_create_id();
}

$user_id = isset($_SESSION['id']) ? $_SESSION['id'] : null;
$session_id = $_SESSION['session_id'];

if (!isset($_GET['id']) || !isset($_GET['action'])) {
    header("Location: add-to-cart.php");
    exit;
}

$cart_item_id = intval($_GET['id']);
$action = $_GET['action'];

if ($user_id) {
    $stmt = $conn->prepare("SELECT quantity FROM cart_items WHERE id = ? AND user_id = ?");
    $stmt->bind_param("ii", $cart_item_id, $user_id);
} else {
    $stmt = $conn->prepare("SELECT quantity FROM cart_items WHERE id = ? AND session_id = ?");
    $stmt->bind_param("is", $cart_item_id, $session_id);
}

$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $item = $result->fetch_assoc();
    $current_quantity = (int)$item['quantity'];

    if ($action === 'plus') {
        $new_quantity = $current_quantity + 1;
    } elseif ($action === 'minus' && $current_quantity > 1) {
        $new_quantity = $current_quantity - 1;
    } else {
        $new_quantity = $current_quantity;
    }

    if ($user_id) {
        $update_stmt = $conn->prepare("UPDATE cart_items SET quantity = ? WHERE id = ? AND user_id = ?");
        $update_stmt->bind_param("iii", $new_quantity, $cart_item_id, $user_id);
    } else {
        $update_stmt = $conn->prepare("UPDATE cart_items SET quantity = ? WHERE id = ? AND session_id = ?");
        $update_stmt->bind_param("iis", $new_quantity, $cart_item_id, $session_id);
    }

    $update_stmt->execute();
    $update_stmt->close();
}

$stmt->close();
$conn->close();

header("Location: add-to-cart.php");
exit;

VaKeR 2022