Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created May 25, 2017 11:19
Show Gist options
  • Save rintoug/9af225504826fbf1804436532e9faeb6 to your computer and use it in GitHub Desktop.
Save rintoug/9af225504826fbf1804436532e9faeb6 to your computer and use it in GitHub Desktop.
Simple PHP Shopping Cart
<?php
//Add to cart
if($action=='addcart' && $_SERVER['REQUEST_METHOD']=='POST') {
//Finding the product by code
$query = "SELECT * FROM products WHERE sku=:sku";
$stmt = $conn->prepare($query);
$stmt->bindParam('sku', $_POST['sku']);
$stmt->execute();
$product = $stmt->fetch();
$currentQty = $_SESSION['products'][$_POST['sku']]['qty']+1; //Incrementing the product qty in cart
$_SESSION['products'][$_POST['sku']] =array('qty'=>$currentQty,'name'=>$product['name'],'image'=>$product['image'],'price'=>$product['price']);
$product='';
header("Location:shopping-cart.php");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment