Simple Shopping Cart using PHP and MySQL

Demo Download

In this tutorial, we will create a Simple Shopping Cart using PHP and MySQL. Purpose of this tutorial is to explain the basic concept of shopping cart, and how to use PHP session to store values into array of cart.

eCommerce websites usually uses the PHP session to store items that are added by the user into cart.

What is Shopping Cart?

On the internet the shopping cart is simply an online application which is available over the internet on e-commerce websites, the visitors on the website can select and add different items into cart which they are intended to buy online.

So lets start creating a simple shopping cart using PHP and MySQLi.

Steps to Create a Simple Shopping Cart using PHP and MySQL

I have divided this tutorial into few steps to make it easier to understand its working.

  1. Create a Database, Table and Dump Sample Data
  2. Create a Database Connection
  3. Create an Index File
  4. Create a Cart File
  5. Create a CSS File

1. Create a Database, Table and Dump Sample Data

To create database run the following query in MySQL.

To create a table run the following query.

Note: I have already attached the SQL file of this table with dummy data, just download the complete zip file of this tutorial.

2. Create a Database Connection

Create a db.php file and paste the following database connection in it. Make sure that you update these credentials with your database credentials.

3. Create an Index File

Create an  index.php file and paste the following script in the beginning of your file.

The above script is just adding values of selected item into array so that we can display them into cart.php page.

Add the following script in the same file in body section to display a cart icon.

Add the following script in the same file after adding the above script to display products from database and display message after adding any product.

4. Create a Cart File

Create a  cart.php file and paste the following script in the beginning of file.

The above script is performing two different things based on chosen action. If user click on remove item button, it will remove item from the cart. And if user changes the selected item quantity, it will also update the item quantity in the session array. If you want to display cart icon here so you can do the same which we did above in  index.php file.

Add the following script in the body section of the  cart.php file.

The above script is simply displaying the products with full details, its price, units, image, and total amount. In here user can also select the quantity of its products. All messages of removing and adding quantity will also display here in the bottom. Sample screenshot is also attached below:

5. Create a CSS File

Create an  style.css file and paste the following style in it.

I try my best to explain this tutorial as simple as possible but if you still have any query you can leave it in comment section below, i will try to respond as soon as possible.

Demo Download

If you found this tutorial helpful, share it with your friends and developers group.

I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook and share it.

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.


  1. Hi Javed, I have a few questions to ask,
    if (isset($_POST[‘action’]) && $_POST[‘action’]==”change”)
    what is $_POST[‘action] I don’t see any thing called action.

    if (isset($_POST[‘code’]) && $_POST[‘code’]!=””)
    what is and where is $_POST[‘code’] came from.

    Thank you for your help , hope to get your help soon.

    1. you want to send parameter value as “change” in postman
      if (isset($_POST[‘name’]) ==”change”)
      in postman you should send

      1. Do i sending any input field that have name as “name”? I am not sending any input field that name is used as name. When i am not sending anything so how can i get it? I am not able to understand what issue you are facing.

  2. Good day sir
    Please, I want to ask if I have 3 items displayed at the same time and I want my user to only select one and make payment based on the one selected and also get privileges based on the one selected. How do I do that e.g web hosting company selling hosting?

    1. Well, yes you can do it, all you need is to remove feature of add items, you will need to place restriction if one item is added hide add to cart button from the options, and for privilege you can make another table in database, add those privilege based on item id and display them once user selected that item in cart.

  3. Great tutorial!

    How would I go about adding a size selection form on the index.php and cart.php pages? In other words if I was selling T-shirt’s how could users select/change the size?


    1. You will require payment integration, it depends what kind of payment solution you are providing, there are several payment options available with integration guide, so choose any one and integrate with your website.

    1. Dear Andrea,

      Images are not stored in Database, only image name and locations may be stores in database, while the image files will be uploaded on your host, then you can display images by calling their name and location from database.

  4. Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/cake/customer/cartproduct.php on line 64
    I get one of the following error messages in below code, why?

    $image_query = mysqli_query($connection,”SELECT * FROM products“);
    while($row = mysqli_fetch_assoc($image_query)){
    echo ”

    Buy Now


  5. Wow Javed, you have done a great job, in which i could code this from the start like you, am really really willing to learn PHP in a full level. But Javed, the project is not complete, how can i submit it to the database?

Leave a Reply

Your email address will not be published. Required fields are marked *