August 13, 2016
Creating an Online Shopping Cart in PHP & MySQL

In this tutorial I show you how to create a simple online shopping cart step by step, you can download this and rapidly modify it for your needs. It will help you to show the list of products, product information, add the product, remove the product, update the product quantity in the cart and view at checkout of your items. The shopping cart items are saved in a session. We can clear this session by removing product items from the shopping cart.
Creating an Online Shopping Cart in PHP & Mysql Part-1
Creating an Online Shopping Cart in PHP & Mysql Part-2
Database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
-- Database: `tut` -- -- Table structure for table `products` -- CREATE TABLE `products` ( `id` int(11) NOT NULL, `p_name` varchar(255) NOT NULL, `image` varchar(255) NOT NULL, `price` double(10,2) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `products` -- INSERT INTO `products` (`id`, `p_name`, `image`, `price`) VALUES (1, 'Apple iPhone 6s', 'apple_6s.png', 649.00), (2, 'Apple Watch', 'apple_watch.jpg', 640.00), (3, 'iPad', 'ipad.jpg', 599.00), (4, 'Macbook Pro', 'macbook_pro.jpg', 1299.00); -- -- Indexes for table `products` -- ALTER TABLE `products` ADD PRIMARY KEY (`id`); |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php session_start(); $connect = mysqli_connect("localhost", "root", "", "tut"); ?> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>SoftAOX Tutorial | Creating an Online Shopping Cart in PHP & Mysql</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </head> <body> <div class="container" style="width:60%;"> <h2 align="center">SoftAOX Tutorial | Creating an Online Shopping Cart in PHP & Mysql</h2> <?php $query = "SELECT * FROM products ORDER BY id ASC"; $result = mysqli_query($connect, $query); if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_array($result)) { ?> <div class="col-md-3"> <form method="post" action="shop.php?action=add&id=<?php echo $row["id"]; ?>"> <div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center"> <img src="<?php echo $row["image"]; ?>" class="img-responsive"> <h5 class="text-info"><?php echo $row["p_name"]; ?></h5> <h5 class="text-danger">$ <?php echo $row["price"]; ?></h5> <input type="text" name="quantity" class="form-control" value="1"> <input type="hidden" name="hidden_name" value="<?php echo $row["p_name"]; ?>"> <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>"> <input type="submit" name="add" style="margin-top:5px;" class="btn btn-default" value="Add to Bag"> </div> </form> </div> <?php } } ?> <div style="clear:both"></div> <h2>My Shopping Bag</h2> <div class="table-responsive"> <table class="table table-bordered"> <tr> <th width="40%">Product Name</th> <th width="10%">Quantity</th> <th width="20%">Price Details</th> <th width="15%">Order Total</th> <th width="5%">Action</th> </tr> <?php if(!empty($_SESSION["cart"])) { $total = 0; foreach($_SESSION["cart"] as $keys => $values) { ?> <tr> <td><?php echo $values["item_name"]; ?></td> <td><?php echo $values["item_quantity"] ?></td> <td>$ <?php echo $values["product_price"]; ?></td> <td>$ <?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?></td> <td><a href="shop.php?action=delete&id=<?php echo $values["product_id"]; ?>"><span class="text-danger">X</span></a></td> </tr> <?php $total = $total + ($values["item_quantity"] * $values["product_price"]); } ?> <tr> <td colspan="3" align="right">Total</td> <td align="right">$ <?php echo number_format($total, 2); ?></td> <td></td> </tr> <?php } ?> </table> </div> </div> </body> </html> |
shop.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php session_start(); $connect = mysqli_connect("localhost", "root", "", "tut"); if(isset($_POST["add"])) { if(isset($_SESSION["cart"])) { $item_array_id = array_column($_SESSION["cart"], "product_id"); if(!in_array($_GET["id"], $item_array_id)) { $count = count($_SESSION["cart"]); $item_array = array( 'product_id' => $_GET["id"], 'item_name' => $_POST["hidden_name"], 'product_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["cart"][$count] = $item_array; echo '<script>window.location="index.php"</script>'; } else { echo '<script>alert("Products already added to cart")</script>'; echo '<script>window.location="index.php"</script>'; } } else { $item_array = array( 'product_id' => $_GET["id"], 'item_name' => $_POST["hidden_name"], 'product_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["cart"][0] = $item_array; } } if(isset($_GET["action"])) { if($_GET["action"] == "delete") { foreach($_SESSION["cart"] as $keys => $values) { if($values["product_id"] == $_GET["id"]) { unset($_SESSION["cart"][$keys]); echo '<script>alert("Product has been removed")</script>'; echo '<script>window.location="index.php"</script>'; } } } } ?> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]