├── README.md ├── create-products.sql ├── images ├── camera.jpg ├── iphone.jpg └── watch.jpg └── shopping-cart.php /README.md: -------------------------------------------------------------------------------- 1 | # Simple-PHP-Shopping-Cart 2 | Simple PHP shopping cart application for beginners. 3 | 4 | ![Shopping Cart Icon](https://oneweb.tools/images/cart.png)
5 | [Source](https://oneweb.tools) 6 | 7 | 8 | ## Step #1 9 | Run the create-products.sql script and change the connection string in your script to your database. 10 | 11 | Read the tutorial here 12 | https://tutsplanet.com/simple-php-shopping-cart/ 13 | -------------------------------------------------------------------------------- /create-products.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Table structure for table `products` 3 | -- 4 | 5 | CREATE TABLE IF NOT EXISTS `products` ( 6 | `product_id` int(11) NOT NULL, 7 | `name` varchar(100) NOT NULL, 8 | `sku` varchar(14) NOT NULL, 9 | `price` decimal(15,2) NOT NULL, 10 | `image` varchar(50) NOT NULL, 11 | PRIMARY KEY (`product_id`), 12 | UNIQUE KEY `sku` (`sku`) 13 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; 14 | 15 | 16 | 17 | -- 18 | -- Dumping data for table `products` 19 | -- 20 | 21 | INSERT INTO `products` (`product_id`, `name`, `sku`, `price`, `image`) VALUES 22 | (1, 'Iphone', 'IPHO001', '400.00', 'images/iphone.jpg'), 23 | (2, 'Camera', 'CAME001', '700.00', 'images/camera.jpg'), 24 | (3, 'Watch', 'WATC001', '100.00', 'images/watch.jpg'); 25 | -------------------------------------------------------------------------------- /images/camera.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rintoug/Simple-PHP-Shopping-Cart/2b8e957ab1c2510d3792b6dd2b3c323ca0eef968/images/camera.jpg -------------------------------------------------------------------------------- /images/iphone.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rintoug/Simple-PHP-Shopping-Cart/2b8e957ab1c2510d3792b6dd2b3c323ca0eef968/images/iphone.jpg -------------------------------------------------------------------------------- /images/watch.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rintoug/Simple-PHP-Shopping-Cart/2b8e957ab1c2510d3792b6dd2b3c323ca0eef968/images/watch.jpg -------------------------------------------------------------------------------- /shopping-cart.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 11 | 12 | 13 | //get action string 14 | $action = isset($_GET['action'])?$_GET['action']:""; 15 | 16 | //Add to cart 17 | if($action=='addcart' && $_SERVER['REQUEST_METHOD']=='POST') { 18 | 19 | //Finding the product by code 20 | $query = "SELECT * FROM products WHERE sku=:sku"; 21 | $stmt = $conn->prepare($query); 22 | $stmt->bindParam('sku', $_POST['sku']); 23 | $stmt->execute(); 24 | $product = $stmt->fetch(); 25 | 26 | $currentQty = $_SESSION['products'][$_POST['sku']]['qty']+1; //Incrementing the product qty in cart 27 | $_SESSION['products'][$_POST['sku']] =array('qty'=>$currentQty,'name'=>$product['name'],'image'=>$product['image'],'price'=>$product['price']); 28 | $product=''; 29 | header("Location:shopping-cart.php"); 30 | } 31 | 32 | //Empty All 33 | if($action=='emptyall') { 34 | $_SESSION['products'] =array(); 35 | header("Location:shopping-cart.php"); 36 | } 37 | 38 | //Empty one by one 39 | if($action=='empty') { 40 | $sku = $_GET['sku']; 41 | $products = $_SESSION['products']; 42 | unset($products[$sku]); 43 | $_SESSION['products']= $products; 44 | header("Location:shopping-cart.php"); 45 | } 46 | 47 | 48 | 49 | 50 | //Get all Products 51 | $query = "SELECT * FROM products"; 52 | $stmt = $conn->prepare($query); 53 | $stmt->execute(); 54 | $products = $stmt->fetchAll(); 55 | 56 | ?> 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | PHP registration form 65 | 66 | 67 | 68 | 69 | 70 |
71 | 72 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | $product):?> 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 |
ImageNamePriceQtyActions
$Delete

Total:$

100 | 101 | 106 |
107 |
108 | 109 |
110 |
Lights 111 |
112 |

113 |

$

114 |
115 |

116 | 117 | 118 |

119 |
120 |
121 |
122 |
123 | 124 |
125 |
126 |
127 | 128 | --------------------------------------------------------------------------------