7 Steps to Create Simple Shopping Cart Application in PHP & MySQL
  • Home
  • PHP

7 Steps to Create Simple Shopping Cart Application in PHP & MySQL

This is a simple php shopping cart application in PHP & MySQL to display products, add to cart functionality in php and checkout features and also removing products from cart. This shopping cart application is built with simple php & mysql code.

Building E-Commerce Shopping Cart Website in PHP & MySQL

On this page you will find 2 scripts one is simply basic Shopping Cart System and the other one advanced and more complex System.
If you want to build advanced E-Commerce System, Scroll down.

Are You Excited to Build E-Commerce Shopping Cart on your own?

I've good News for you! Created a Video Course with 7.5 Hours of Content...

Below is the screenshot of all the files that I’m going to create.

1. Creating Template Files

First of all, I’m going to create reusable template files. Create three files with the name of header.php, nav.php, footer.php




After that create index.php file to list all the products


2. Creating Database Table

Next create database table with the name of products, you can use this below sql. I’ve already created few products.

3. Connecting to Database Table

Connecting to database using php, create connect.php file and use this code.

Then include this connect.php file in our index.php file. If you are using above code, I’ve already included it.

4. Displaying Products

Next fetch products from database table using SELECT sql, and display all the products in our shopping cart index.php file.

Replace index.php html code with this code.

5. Add to Cart Functionality

For Cart functionality in our php shopping cart appliation, I’m going to use sessions. I’ll add product item id to session.

Create a php file with the name of addtocart.php. If the get superglobal is set then create session with item id, then redirect user with status success. If any case if the session is not set then it will redirect user to status failed.

Above code will works if only one product is added to cart. If you want to add multiple products then you have to use this below code.

Here I’m creating comma separated list with all product id’s added to cart, If the session is set. Otherwise, previous code will be executed.

If the product is already added to cart, then I’m not going add it again. For that checking cartitems array with in_array function. If the item exists then user will be redirected to status incart. If the item doesn’t exist in session, then that id will be added session.

6. Displaying Cart Items

Create cart.php file, use this below html code.

Reading sesison, creating an array of id using explode function.

Fetching data from database and displaying it in table row.

Displaying total price of all item prices below table row.

7. Deleting Items from cart

Create a file with the name of delcart.php. I’ve already added remove link in cart.php.

Here I’m reading session, creating an array with all the items. Removing array element with pocket number using unset php function.

Complete code of Simple Shopping Cart in PHP & MySQL

If you have any problem arranging above pieces of code. You can use this complete code.

Building E-Commerce Shopping Cart Website in PHP & MySQL

It’s more advanced than the above one and it contains lot of features that you can learn to build E-Commerce Shopping Cart in PHP

Are You Excited to Build E-Commerce Shopping Cart on your own?

I've good News for you! Created a Video Course with 7.5 Hours of Content...

First of all, you need to have basic HTML template for Shopping Cart application. You can use any basic template or use bootstrap. Here I’m using a template if you want to have access to all the source code join my course.

Here I’m going to build a Shopping Cart with these features

Front End

In FrontEnd Displaying Products and on home page displaying products with categories. Next adding products to cart from homepage category page and also adding products to cart in multiple quantities from the single product page.

Next is displaying products added to cart in cart page and also in the drop-down menu with a checkout link.

While user goes to checkout page after adding products to cart. We will check user logged in or not?
If user logged in we will allow him to the checkout page or else he will be prompted to log in or register.
If he is a new user, he should register. If he is an existing user, he can log in with his login credentials.

In checkout page, if the user already has billing details. It will be displayed or else user will be prompted to enter billing details.

After that user can choose the payment method and accept the terms his order will be placed. For now, I’m using only Cash On Delivery method.

If we use payment gateways, the user will be redirected to a payment gateway. If he pays we will store transaction id and other information from payment gateway provider into our database.

A customer can submit reviews to the product, a customer can submit only one review per product.

The customer can cancel his order. Also, a customer can update his or her address from my account page.
A customer can check all the orders on my account page. And also view single orders from single order view page.

Wishlist for the customer to add products to wishlist and remove products from wishlist.

Back End

In Back End Admin Area, Admin User will be able to log in and add, edit & delete products. And also admin user can check the orders. Go through the list of customers and go through all the reviews submitted by customers.

Order Processing from the backend, admin can process the order in 4 different stages one is Order Placed, Order In Progress, Order Dispatched, and Order Delivered.

A lot more features are included.

Create Database & Tables

First of we will start with creating database and tables, we need couple of tables for our features. I’ll create basic tables that we need and we will create other tables as we go along.

First I’ll list out all the tables that we need in our project
admin table
category table
products table
users table
usersmeta table
orders table
orderitmes table
orderstracking table
wishlist table
reviews table

For now, I’ll create first three tables that is admin table and category table and products table. Here is the SQL code for each one of the table.

In Admin table, I’m creating 5 columns with id, first name, last name, email, password.

In Category table, I’m creating with 2 columns id, name.

And I’ll create other tables as we go along with project.

2. Creating Required Files

First I’ll start with creating files in front end
index.php – we need this file for displaying all products and products based on categories
single.php – for displaying single product details
addtocart.php – for adding items to cart
cart.php – for displaying products in cart
delcart.php – for deleting items from cart.
checkout.php – for customer checkout process
login.php – for users to login/register from same page
loginprocess.php – for login functionality of customer
registerprocess.php – for customer registration process.
my-account.php – for displaying all the orders associated with customer
view-order.php – for displaying single order with more details
cancel-order.php – for cancelling the order
edit-order.php –
addtowishlist.php – for adding items to wishlist
wishlist.php – for displaying products in wishlist
delwishlist.php – for deleting products from wishlist
logout.php –

And also we need inc directory for template files that is header, footer and navigation files
Create a directory with the name of inc and create these files
header.php – used for header code in html and it is reusalbe accross all the files
footer.php – used for closing of html file and it is reusable accross all the files
nav.php – used for navigational menu and it is reused in all the files

And also we need one more directory that is config, in this directory I’ll store all the configuration files. For now I’ll create database connection file.

connect.php – We will use this file for connecting to database and selecting database.

Then I’ll move on to creating necessary files in Admin Area

In Admin area, first of all I’ll create directory our reusable template files.
header.php – used for header code in html and it is reusalbe accross all the files in admin area
footer.php – used for closing of html file and it is reusable accross all the files in admin area
nav.php – used for navigational menu and it is reused in all the files in admin area

login.php –
index.php –
caetgories.php –
addcategory.php –
delcategory.php –
editcategory.php –
products.php –
editproduct.php –
delproduct.php –
delprodimg.php –
orders.php –
order-process.php –
customers.php –
reviews.php –
logout.php –

Admin Login

Before working on login functionality we need database connect for that I’ll create

Login Checking on index.php and on all admin pages

Category CRUD in Admin Area





Products CRUD in Admin Area



Form code in editproduct.php




Displaying Product in Front End


Add to Cart Functionality


Customer Login & Registration




Storing Customer Billing Details in Checkout Page


Placing Order by Customer


Are You Excited to Build E-Commerce Shopping Cart on your own?

I've good News for you! Created a Video Course with 7.5 Hours of Content...

If you have any problem, let me know through the comment form below.

php login form create Php shoppin shopping cart php add to cart in php adding items to cart php form php shopping cart php shopping cart tutorial step by step php cart add to cart php add to cart wordpress mysql query in php
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 16 comments

hi bro, i cant login admin at E-Commerce Shopping Cart Script.
can u tell me this password, pls

    Vivek Vengala

    Hi Midchel,

    I’m using password_hash for admin password, generate password_hash in PHP with the password you want.
    Update it in password column, after that you can log in with your new password.



I’m experiencing problems whenever I start a session it always says

Notice: Undefined index: cart in C:\wamp64\www\ecomphp\inc\nav.php on line 34

and I need to add a product to the cart so that it won’t go away. What can I do to solve this? Thank you!

    Vivek Vengala

    Hi Trixia,

    Add this if condition around that div block if(isset($_SESSION[‘cart’])){ }

    Thank you for asking


      Hi, it worked but another problem showed up. I can’t seem to open the dropdown menu, it cuts the navigation menu like this: http://i64.tinypic.com/2h66dfp.png and I can’t get it back to normal unless i remove the condition you mentioned.


        Plus, I can’t view the other items if I add 2 or more in my cart. Only the same item keeps showing up. Thank you so much for your help!

          Vivek Vengala

          It’s because I’m using the orderid=3 manually. Forgot to change it the variable, Change this number to the orderid variable…

        Vivek Vengala

        Otherwise, you can define an empty session cart with the value zero in it…
        This solves the menu problem

      zain ul abidden

      sir kindly explain on which page this code will be insert to avoid above mentioned problem

        Vivek Vengala

        Use that code in nav.php file or define an empty session cart with the value zero in it…


hi sir ..
when i click on view order it always display same order every time like orderid=3
not show my order which i placed

    Vivek Vengala

    Hi Faisal, On that page, I’m using the orderid=3 manually. Just update this number to order id variable it should work…


Hello sir,
All happens good except incnav.php page.It gives( Undefined index: cart in C:xampphtdocsincnav.php on line 34) as well as ( Invalid argument supplied for foreach() in C:xampphtdocsincnav.php on line 47 )errors.Sir kindly help me to fix these errors.Thanks

    Vivek Vengala

    Use this code in nav.php file :


your shopping cart is awsome and reliable.i have a question that what if we have procts of two category as products1 and products2. then how to fix cart issue by submitting these two values into cart page code as you given in cart.php

$value) {
//echo $key . ” : ” . $value[‘quantity’] .””;

$cartsql = “SELECT * FROM products WHERE id=$key”;
$cartres = mysqli_query($connection, $cartsql);
$cartr = mysqli_fetch_assoc($cartres);

    Vivek Vengala

    Hi zain,

    Products are stored in cart session with product id and the quantity not based on the product category. Products stored in Cart session will be displayed in the cart.php page with the product id and the total value will be calculated with the total number of items multiplied by product price.


Leave a Reply: