Build a Shopping Cart With PHP and MySQL

Where to Find the Best PHP Shopping Cart Scripts for 2021
If you’re looking for a quick, ready-made solution, there are many premium PHP shopping cart scripts to choose from on Envato Market. Here’s a quick look at a few of them.

The best PHP shoppin…


This content originally appeared on Envato Tuts+ Tutorials and was authored by Suciu Vlad

Where to Find the Best PHP Shopping Cart Scripts for 2021

If you're looking for a quick, ready-made solution, there are many premium PHP shopping cart scripts to choose from on Envato Market. Here's a quick look at a few of them.

The best PHP shopping carts are on CodeCanyon.The best PHP shopping carts are on CodeCanyon.The best PHP shopping carts are on CodeCanyon.
The best PHP shopping carts are on CodeCanyon.

5 Best PHP Shopping Cart Scripts From CodeCanyon

Before we jump into the shopping cart system PHP tutorial, let's explore some top PHP shopping cart scripts. If you aren't an expert at coding, or want to find a ready-to-use solution, these are for you.

1. FleetCart: PHP Shopping Cart + eCommerce System

FleetCart - PHP Shopping Cart eCommerce SystemFleetCart - PHP Shopping Cart eCommerce SystemFleetCart - PHP Shopping Cart eCommerce System

FleetCart isn't only a PHP shopping cart. It's a full eCommerce system for your online store. This powerful PHP shopping cart script has many cool features. It's fully responsive, multi-language, supports multiple currencies, and more.

With over 1,000 sales and a five-star rating, this is one of our best PHP shopping carts. See how this works in the live preview!

2. LivelyCart: PHP MySQL JQuery Shopping Cart

LivelyCart - PHP MySQL JQuery Shopping CartLivelyCart - PHP MySQL JQuery Shopping CartLivelyCart - PHP MySQL JQuery Shopping Cart

This is an easy-to-use, customizable PHP MySQL jQuery shopping cart. It's equipped with two checkout options using PayPal and Submit order by Email. You can use both or either one. It's a complete PHP shopping cart code download.

3. Digital Paybox: PHP Shopping Cart Script

Digital Paybox - PHP Shopping Cart ScriptDigital Paybox - PHP Shopping Cart ScriptDigital Paybox - PHP Shopping Cart Script

Digital Paybox is a powerful PHP script which allows you to sell digital products directly on your website. The PHP shopping cart handles payments through multiple payment gateways (PayPal, Payza/AlertPay, Skrill/Moneybookers, Authorize.Net, InterKassa, EgoPay, Perfect Money, BitPay, Blockchain.info, and Stripe) and can be easily integrated into any webpage. 

With this PHP shopping cart code download, you don’t have to install complicated shopping carts and create their design to match your website. This is the easiest way to distribute digital products and monetize a website.

4. Stripe Payment Terminal: PHP Shopping Cart

Stripe Payment Terminal - PHP Shopping CartStripe Payment Terminal - PHP Shopping CartStripe Payment Terminal - PHP Shopping Cart

This PHP shopping cart script allows you to have a quick and easy credit card payment terminal for your clients processed by stripe.com. 

Installation and configuration of the script take less than five minutes (however, you do need to have SSL and a stripe.com account).

5. GeniusCart: PHP Shopping Cart + eCommerce System 

GeniusCart - PHP Shopping Cart eCommerce System GeniusCart - PHP Shopping Cart eCommerce System GeniusCart - PHP Shopping Cart eCommerce System

GeniusCart is one of the best PHP shopping carts on CodeCanyon. This PHP MySQL jQuery shopping cart features more than 1,000 sales and a five-star rating.

The PHP shopping cart is a full eCommerce system. You can easily create a single or multivendor store with this solution. You've got to check out the preview to watch it in action!

How to Create a Shopping Cart in PHP and MySQL

Here's a quick video preview on how to build a shopping cart with PHP and MySQL. I'll go over the full process step by step after this preview. You can watch the full shopping cart system PHP tutorial after the steps.

Step 1

Let's begin by taking a look at the folder structure:

Directory StructureDirectory StructureDirectory Structure

Structure

  • reset.css: you can get it reset from this link
  • style.css: our own CSS file that we will be using to style our HTML markup
  • connection.php: the file which will do the database connection
  • index.php: the template for our shopping cart
  • cart.php: the file where we will able to change our products from the cart (add, remove)
  • products.php: the products listing page

Step 2

We'll start by writing the HTML markup and then style it. So open index.php and copy/paste the code below:

As you can see, our page has two columns: the main column and the sidebar. Let's move on to the CSS. Open the style.css file and type the code below:

Here's how our products page should look now:

Product ListingProduct ListingProduct Listing

Step 3

Before we move on to the PHP/MySQL part, we need to create a database. So open phpMyAdmin and follow these steps:

  1. Go to the Privileges tab, click the add new user button, and use the following settings: Username: tutorial; Host: localhost; Password: supersecretpassword;. Now make sure that Global privileges is set; then move on to the next step.
  2. Create a new database called tutorials.
  3. Create a new table called products and set the number of fields to 4. Now populate those fields so you have: id_integer - make sure it's set to INT and mark it as PRIMARY(also set it to auto_increment); name - make it VARCHAR with a length of 100; description - VARCHAR with a length of 250; price - make sure it's set to DECIMAL(2,6).
  4. Populate your table with some sample products.

To save some time, I've exported my products table so that you can simply run the following query:

Add a New UserAdd a New UserAdd a New User
Create a DatabaseCreate a DatabaseCreate a Database
Table StructureTable StructureTable Structure

Step 4

Before we move and select data from the database, I'll make my index.php a template for the product list and cart. So add the following code to the top of your index.php page:

  1. session_start() - this is for later use; it will allow us to actually use sessions (it's vital that the session_start is written before any other data is sent to the browser).
  2. On the second line, we include the connection.php which will establish the connection to the database (we will deal with this in a second). One more thing: the difference between include and require is that if you use require and the file can't be found, the script execution will end. If you use include, the script will continue working.
  3. Instead of copying the entire HTML code (the link to the CSS and to the JS) for every file in your site, you could just make them all relative to a single file. So first, I'm checking if there's a GET variable called "page". If it's not, I'm creating a new variable called $_page. If the GET variable called "page" is set first, I want to make sure that the file which I'm going to include is a valid page.

To make this work, we need to include the file; add this line to the index.php file between the div which has id attribute set to main:

Now here's the complete index.php:

Code PreviewCode PreviewCode Preview

Let's create the connection to MySQL. Open connections.php and type the following:

Step 5

Now it's time to write the markup for the products page. So go ahead and open it and type the following:

Let's take a look at the page:

Products PageProducts PageProducts Page

As you can see, it's pretty ugly. So let's style it by adding this extra CSS.

Okay: let's see how it looks now:

Product Listing With CSSProduct Listing With CSSProduct Listing With CSS

Looks a lot better, doesn't it? Below you have the complete style.css code:

Step 6

Before we extract the product from the database, let's remove the last two table rows from our table (we used them only to see what our table would look like). Remove this:

Great! Now replace the table rows with the following PHP code:

  1. So first we use SELECT to retrieve the products, and then we loop through each row from the database and display it to the page in a table row.
  2. You can see that the anchor links to the same page (when the user clicks the anchor link, the product will be added to the cart/session). We just pass some extra variables like the id of the product.

If you hover one of the add to cart links, you can see, at the bottom of the page, the id of the product is passed as a querystring parameter.

Querystring Preview

Step 7

Let's make that anchor link work by adding the following code at the top of our page:

  1. If the GET variable called action is set and its value is add, we execute the code.
  2. We make sure that the id which is passed through the $_GET variable is an integer.
  3. If the id of the product is already in the $_SESSION variable, we just increment the quantity by one.
  4. If the id is not in the session, we need to make sure that the id which is passed through the $_GET variable exists in the database. If it does, we grab the price and create its session. If it doesn't, we set a variable called $message which stores the error message.

Let's check if the $message variable is set and display it on the page (type this code under the H1 page title):

Here you can see the complete products.php page.

Here's the error message if the id of the product is invalid.

Error Message PreviewError Message PreviewError Message Preview

Step 8

Let's go back to the index.php file and build the sidebar. Add the following code:

  1. Firstly, we check if the session cart is set. If it's not, we display the message, alerting the user that the cart is empty.
  2. Next, we select all the products from the session and display it.

Take a look at the pictures below:

Cart PreviewCart PreviewCart Preview

Since the index.php file is a template for all the files, the sidebar will be visible in the cart.php too. Cool, right?

Step 9

Finally, open cart.php and start by typing the following code:

The code is similar to the one from index.php and products.php, so I'm not going to explain everything again. You should notice that instead of displaying the quantity in a form, now it's displayed in an input box (so that we can change the quantity). Also, the table is wrapped in a form tag. To get the total price of the items, we multiply the quantity of the specific product (from the session) with its price. This is done in a foreach loop.

NOTE: The input is an array, the key is the id of the product, and the quantity is the quantity value.

Cart PreviewCart PreviewCart Preview

Step 10

The last step we need to do is to make the form work. So add this code to the top of the cart.php page.

  1. Firstly, we check if the form was submitted. If it was submitted and the value of the input was zero, we unset that session.
  2. If the value is any other value than zero, we set the quantity to that value instead.

Here is the complete cart.php file.

Cart PreviewCart PreviewCart Preview

I hope that you enjoyed this tutorial. If you have any questions, be sure to watch the more in-depth video tutorial!

Watch How to Create a Shopping Cart in PHP and MySQL

You've seen the step-by-step process and the PHP shopping cart sample code. Now, here's the full video tutorial on how to create a simple shopping cart using PHP & MySQL. 

Discover More PHP Scripts and Resources

I hope you liked the PHP online store tutorial and understand the process of creating a simple shopping cart using PHP and MySQL. If you'd like to explore more useful resources, take a look at this:

This post has been updated with contributions from Maria Villanueva and Sajal Soni. Maria is a staff writer with Envato Tuts+. Sajal belongs to India, and he loves to spend time creating websites based on open source frameworks.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Suciu Vlad


Print Share Comment Cite Upload Translate Updates
APA

Suciu Vlad | Sciencx (2021-04-26T08:07:29+00:00) Build a Shopping Cart With PHP and MySQL. Retrieved from https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/

MLA
" » Build a Shopping Cart With PHP and MySQL." Suciu Vlad | Sciencx - Monday April 26, 2021, https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/
HARVARD
Suciu Vlad | Sciencx Monday April 26, 2021 » Build a Shopping Cart With PHP and MySQL., viewed ,<https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/>
VANCOUVER
Suciu Vlad | Sciencx - » Build a Shopping Cart With PHP and MySQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/
CHICAGO
" » Build a Shopping Cart With PHP and MySQL." Suciu Vlad | Sciencx - Accessed . https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/
IEEE
" » Build a Shopping Cart With PHP and MySQL." Suciu Vlad | Sciencx [Online]. Available: https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/. [Accessed: ]
rf:citation
» Build a Shopping Cart With PHP and MySQL | Suciu Vlad | Sciencx | https://www.scien.cx/2021/04/26/build-a-shopping-cart-with-php-and-mysql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.