Today I going to share the PHP shopping cart program. In other words, add to cart feature in PHP and MySQL. You had seen add to cart feature in very big and well-known e-commerce platform like Amazon, Flipkart, etc. Basically, the cart button creates a list of products, that you want to buy. Cart program is full of calculation in the backend.
Today I am sharing a simple PHP shopping cart program example and source code. After visit this post, you will able to create add to cart feature in PHP & MySQL. This program is very simple, but it looks pretty good. Because of this program’s structure is built-in bootstrap. I used bootstrap for an attractive and responsive look.
In this add to cart program, I did not use the separate CSS file, I put some in-line CSS. So, There is Just one file and folder for store images. MySQL fetches images from the image folder. One thing I want to explain: MySQL doesn’t store images in the database, SQL store only image name in the database. That’s why you have to create a folder to store images.
Preview Of This Add To Cart Program
First, see this preview of the program. If you like, then to go for source code.
So, this is the preview. I think this program looks not bad. For beginners, this program will be very informative. Because If you do search for this program most of will in any PHP framework. That will be difficult to understand. Now time to share code.
You May Also Like:
PHP Shopping Cart Programs Source Code
As always, Before sharing source code I want to say about this program. For this program, I used PHP, MySQL, and Bootstrap. create a database is very important, without the database this program is a dummy. MySQL create this program dynamic. In the database, we will store the product name, ID, product price, & product image path.
You have to do 3 things: Create a database named ‘cart‘, create an ‘index.php‘ file, and the final thing create a folder and store product images.
Create Images Folder
Create a folder named ‘images‘ and put all your products images there. This is a very important thing in this program. And rename all images with simple names like ‘1jpg’ or ‘product1.jpg’. because of these types of simple names is easy to put in MySQL database. Otherwise, if your images have a long name that’s will be difficult to remember names, then you have to copy and paste names.
Create Database
Create a database named ‘cart’ because in MySQL connection I had put database name cart. If you create a database with another name you also have to change the database name in PHP file. After successfully creating database go to SQL command section and put these codes given below.
1 2 3 4 5 6 |
CREATE TABLE `tbl_product` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `image` varchar(255) NOT NULL, `price` double(10,2) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; |
This code will create a table with required fields. Now you can insert new products in your database. For add products, go to your database located in PhpMyAdmin, and go to insert section. Now there you can add products by giving their products info like ID, name, price & image name.
Create PHP File
Now you have to create a PHP file named ‘index.php’ or any name for giving life to this program. After creating file put these codes given here below.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
<?php /* code by webdevtrick ( https://webdevtrick.com ) */ session_start(); $connect = mysqli_connect("localhost", "root", "", "cart"); if(isset($_POST["add_to_cart"])) { if(isset($_SESSION["shopping_cart"])) { $item_array_id = array_column($_SESSION["shopping_cart"], "item_id"); if(!in_array($_GET["id"], $item_array_id)) { $count = count($_SESSION["shopping_cart"]); $item_array = array( 'item_id' => $_GET["id"], 'item_name' => $_POST["hidden_name"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["shopping_cart"][$count] = $item_array; } else { echo '<script>alert("Item Already Added")</script>'; } } else { $item_array = array( 'item_id' => $_GET["id"], 'item_name' => $_POST["hidden_name"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["shopping_cart"][0] = $item_array; } } if(isset($_GET["action"])) { if($_GET["action"] == "delete") { foreach($_SESSION["shopping_cart"] as $keys => $values) { if($values["item_id"] == $_GET["id"]) { unset($_SESSION["shopping_cart"][$keys]); echo '<script>alert("Item Removed")</script>'; echo '<script>window.location="index.php"</script>'; } } } } ?> <!DOCTYPE html> <html> <head> <title>Shopping Cart In PHP and MySql | Webdevtrick.com</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> </head> <body> <br /> <div class="container"> <br /> <br /> <br /> <h3 align="center">Shoping Cart With PHP And MySql | Source Code By <a href="https://webdevtrick.com">Webdevtrick.com</a></h3><br /> <br /><br /> <?php $query = "SELECT * FROM tbl_product 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-4"> <form method="post" action="index.php?action=add&id=<?php echo $row["id"]; ?>"> <div style="border:3px solid #5cb85c; background-color:whitesmoke; border-radius:5px; padding:16px;" align="center"> <img src="images/<?php echo $row["image"]; ?>" class="img-responsive" /><br /> <h4 class="text-info"><?php echo $row["name"]; ?></h4> <h4 class="text-danger">$ <?php echo $row["price"]; ?></h4> <input type="text" name="quantity" value="1" class="form-control" /> <input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" /> <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" /> <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" /> </div> </form> </div> <?php } } ?> <div style="clear:both"></div> <br /> <h3>Order Details</h3> <div class="table-responsive"> <table class="table table-bordered"> <tr> <th width="40%">Item Name</th> <th width="10%">Quantity</th> <th width="20%">Price</th> <th width="15%">Total</th> <th width="5%">Action</th> </tr> <?php if(!empty($_SESSION["shopping_cart"])) { $total = 0; foreach($_SESSION["shopping_cart"] as $keys => $values) { ?> <tr> <td><?php echo $values["item_name"]; ?></td> <td><?php echo $values["item_quantity"]; ?></td> <td>$ <?php echo $values["item_price"]; ?></td> <td>$ <?php echo number_format($values["item_quantity"] * $values["item_price"], 2);?></td> <td><a href="index.php?action=delete&id=<?php echo $values["item_id"]; ?>"><span class="text-danger">Remove</span></a></td> </tr> <?php $total = $total + ($values["item_quantity"] * $values["item_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> </div> <br /> </body> </html> |
That’s It. Now You have successfully created a shopping cart program in PHP. I know this process is a little bit difficult for beginners If you don’t understand how will create the complete program. Don’t worry I am giving you full source code file in ZIP format.
ZIP Password: webdevtrick.comÂ
I hope you like this post. If you have any doubt or question comment down below. I will reply you as soon as possible.
Thanks For Visiting, Keep Visiting.
Good
Thank you, Stay Connected.
Very helpful. Is there anything to link this program to a checkout
thank you
how can i add those cart detain in another table??
how can we store price in hidden filed..if someone change it then how can we handle this
where is $_SESSION[“shopping_cart”] came from?
I want the user to be able to pay using paypal or credit card
set payment gateway
Dear Sir,
I need to show products in drop down list and into the list add-to-cart button is it possible? I became very grateful to you if possible to do this….. Thanks
Please, would you give me zip password? I’m still learning about this. (webdevtrick.com) that doesn’t work as a password to fill.
Thanks alot
Hello,
Am facing this error.
Fatal error: Call to undefined function array_column() in C:\wamp\www\shopping-cart-php\index.php on line 7
while adding a button for payment gateway
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\website\afterlogin\carts.php:83) in C:\xampp\htdocs\website\afterlogin\carts.php on line 147
Requesting for the the zip password the latter isn’t working .Thank you.
thanks sir. please you can do also a tutoial on inserting the data rom shopping cart to a database
I have updated all your code, SQL needed some adjustments also. Anyways, ty for it!
Updated version on my git:
https://github.com/SHJordan/shopping-cart-php
Thank u very much bro I appreciate you work good job..
Its good that you shared the source code. Some people would charge for that also. But beginners like us, know the value of this code when shared for free. And we are grateful for that.
How can I change those currency dollor($) to INR? pls help!
very helpful article
Thankyou, that’s very helpful
I get an issue: I add some items to my cart. I then delete of of those addede items. It updates the cart correctly. BUT – if I try to add another/a new item to the cart it doesn’t get added correctly – in stead it replaces one of the original items in the cart?! Does anyone experience the same – and have a fix? Highly appreciated!