prepare('SELECT product_code FROM cart WHERE product_code=?');
$stmt->bind_param('s',$pcode);
$stmt->execute();
$res = $stmt->get_result();
$r = $res->fetch_assoc();
$code = $r['product_code'] ?? '';
if (!$code) {
$query = $conn->prepare('INSERT INTO cart (product_name,product_price,product_image,qty,total_price,product_code) VALUES (?,?,?,?,?,?)');
$query->bind_param('ssssss',$pname,$pprice,$pimage,$pqty,$total_price,$pcode);
$query->execute();
echo '
Item added to your cart!
';
} else {
echo '
Item already added to your cart!
';
}
}
// Get no.of items available in the cart table
if (isset($_GET['cartItem']) && isset($_GET['cartItem']) == 'cart_item') {
$stmt = $conn->prepare('SELECT * FROM cart');
$stmt->execute();
$stmt->store_result();
$rows = $stmt->num_rows;
echo $rows;
}
// Remove single items from cart
if (isset($_GET['remove'])) {
$id = $_GET['remove'];
$stmt = $conn->prepare('DELETE FROM cart WHERE id=?');
$stmt->bind_param('i',$id);
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'Item removed from the cart!';
header('location:cart.php');
}
// Remove all items at once from cart
if (isset($_GET['clear'])) {
$stmt = $conn->prepare('DELETE FROM cart');
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'All Item removed from the cart!';
header('location:cart.php');
}
// Set total price of the product in the cart table
if (isset($_POST['qty'])) {
$qty = $_POST['qty'];
$pid = $_POST['pid'];
$pprice = $_POST['pprice'];
$tprice = $qty * $pprice;
$stmt = $conn->prepare('UPDATE cart SET qty=?, total_price=? WHERE id=?');
$stmt->bind_param('isi',$qty,$tprice,$pid);
$stmt->execute();
}
// Checkout and save customer info in the orders table
if (isset($_POST['action']) && isset($_POST['action']) == 'order') {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$products = $_POST['products'];
$grand_total = $_POST['grand_total'];
$address = $_POST['address'];
$pmode = $_POST['pmode'];
$data = '';
$stmt = $conn->prepare('INSERT INTO orders (name,email,phone,address,pmode,products,amount_paid)VALUES(?,?,?,?,?,?,?)');
$stmt->bind_param('sssssss',$name,$email,$phone,$address,$pmode,$products,$grand_total);
$stmt->execute();
$stmt2 = $conn->prepare('DELETE FROM cart');
$stmt2->execute();
$data .= '
Thank You!
Your Order Placed Successfully!
Items Purchased : ' . $products . '
Your Name : ' . $name . '
Your E-mail : ' . $email . '
Your Phone : ' . $phone . '
Total Amount Paid : ' . number_format($grand_total,2) . '
Payment Mode : ' . $pmode . '
';
echo $data;
}
?>