%PDF- %GIF98; %PNG;
Server : ApacheSystem : 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/user/ |
Upload File : |
<?php
session_start();
include('../dbcon.php');
if (isset($_POST['add_to_cart'])) {
$product_id = intval($_POST['product_id']);
$quantity = intval($_POST['quantity']);
$user_id = intval($_POST['user_id']);
if ($user_id <= 0) {
echo "<script>alert('Please log in to add products to the cart.'); window.location.href = 'login.php';</script>";
exit;
}
$stmt = $conn->prepare("SELECT * FROM cart_items WHERE user_id = ? AND product_id = ?");
$stmt->bind_param("ii", $user_id, $product_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$stmt = $conn->prepare("UPDATE cart_items SET quantity = quantity + ? WHERE user_id = ? AND product_id = ?");
$stmt->bind_param("iii", $quantity, $user_id, $product_id);
} else {
$stmt = $conn->prepare("INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)");
$stmt->bind_param("iii", $user_id, $product_id, $quantity);
}
if ($stmt->execute()) {
echo "<script>alert('Product added to cart successfully.'); window.location.href = 'mycartshow.php';</script>";
} else {
echo "<script>alert('Error adding product to cart. Please try again.');</script>";
}
$stmt->close();
$conn->close();
}
?>