Skip to main content

How to Build Shopping Cart w/ Checkout in PHP

Language


I just rewrite the code included in this post to fix minor problem encounter by other PHP programmers who can’t run the shopping cart.

The code is taken from this link: http://www.qualitycodes.com/tutorial.php?articleid=25&title=Tutorial-Building-a-shopping-cart-in-PHP.

As you may have noticed, a lot of programmers are screaming for help on how to fix some bugs on this tutorial. Since I cannot upload the code in the said website, I decided to post it here. Still credit goes to “Richard Clark”.

Changes on the code are following:

  1. Change “<?” to “<?php” to handle php correctly. Some old program still uses “<?” to run PHP code. However, PHP 5 is not stricter and requires you to use php after <?, unless you configure Apache to allow such a shorthand support.
  2. Add echo command instead of equal (=) sign.

Note: If you run the code prior to downloading this code, make sure to clear your browser cache.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted bySpiklee (not verified)on Thu, 04/05/2012 - 00:04

Hello,

I've tried uploading the files directly to my web server, but it still produces errors and not the shopping cart. Is there something I should be doing differently such as uploading the sql file elsewhere?

Submitted byAnonymous (not verified)on Wed, 08/29/2012 - 18:49

sir thanks for this awesome tutioral....i realy appreciate you but i have a problem......i have three product categories....so i had used three products table,three products page, 3 shopping cart pages.......but the problem when only one product works ........but when i open second page.........nd select add to cart........shopping cart does not get name,price,description...only show quantinty and serial.........other fields remain blank...........although other option like update,clear,and remove is working properly..............please help me :(

Submitted byAnonymous (not verified)on Tue, 10/09/2012 - 19:51

this was extremely helpful. I am trying to make my first e-comm project.. it was a great help. thanks.

Submitted byAnonymous (not verified)on Sat, 10/13/2012 - 15:18

Thanx!! a lot for this readymade scripting of shopping cart..
Thanx........

Submitted byraiin (not verified)on Wed, 10/31/2012 - 12:50

hi...can you explain this...
include("includes/db.php");
include("includes/functions.php");
please...help me..

Submitted byjavac (not verified)on Fri, 09/06/2013 - 11:57

In reply to by raiin (not verified)

You need to include the external php file e.g. db.php, functions.php into your main php file, making an external php file will make your code more organize..

Submitted byrenee (not verified)on Mon, 11/05/2012 - 10:36

Hello Sir,

Quick question if you can please help me. I want to pass a variable to the shoppingcart.php coming from products.php so that when the customer clicks on "Continue Shopping" it will put the customer back to the products page based on that variable. Basically I call the products page like this for example the $dealID value is 37 "products.php?dealID=37". That part works just fine my problem is when I put the item on the shopping cart when I click on continue shopping I need it to go back to "products.php?dealID=37" but I cant seem to make the shopping cart load the $dealID variable

I hope you can help me with this.

Submitted byseng fong (not verified)on Sun, 11/25/2012 - 18:07

when i going to the shoppingcart.php it come out those errors. Please help ASAP..thanks

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 5

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 8

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 11

Notice: Undefined variable: msg in
C:\xampp\htdocs\cart\shoppingcart.php on line 63

Submitted byAnonymous (not verified)on Mon, 03/04/2013 - 12:57

Good day. Im blessed to have your free ready code .
Thanks a lot it really helps to our project in web :)

Submitted byAnonymous (not verified)on Tue, 03/05/2013 - 01:26

How should i link the shopping cart to the email?
After checkout it should send a mail with all the items in the cart instaed of going for payment.
Please help.

Submitted byAnonymous (not verified)on Mon, 04/08/2013 - 02:05

pls tell me how to solve. this ( ! ) Notice: Undefined index: command in C:\wamp\www\shopping cart_rtmh\shoppingcart.php on line 4 how to solve.
Notice: Undefined index: cart in C:\wamp\www\shopping cart_rtmh\shoppingcart.php

Submitted byAnonymous (not verified)on Sun, 04/14/2013 - 14:29

many many thanks

Submitted byAnonymous (not verified)on Fri, 04/26/2013 - 13:18

Notice: Undefined index: command in C:\wamp\www\products.php on line 5
Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 5

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 8

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 11

Notice: Undefined variable: msg in
C:\xampp\htdocs\cart\shoppingcart.php on line 63
Notice: Undefined index: command in C:\wamp\www\shopping cart_rtmh\shoppingcart.php on line 66

Submitted bytony_the greAT (not verified)on Wed, 11/27/2013 - 22:28

In reply to by Anonymous (not verified)

Same problem here
Notice: Undefined index: command in C:\wamp\www\try\products.php on line 5

Submitted bykamran (not verified)on Mon, 07/29/2013 - 20:52

i want to apply coupon code option any one help me ?

Submitted byAnonymous (not verified)on Fri, 08/09/2013 - 16:52

How to put Paypal Checkout, Skrill Checkout, and Bank Wire Transfer which is integrated with this shopping cart?
How to create a php file of it?
Thank you very much.

Submitted byAnonymous (not verified)on Fri, 09/06/2013 - 17:35

looks good

Submitted bynavi (not verified)on Mon, 10/07/2013 - 17:16

Notice: Undefined index: command in C:\wamp\www\products.php on line 5
Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 5

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 8

Notice: Undefined index: command in C:\xampp\htdocs\cart\shoppingcart.php on line 11

Notice: Undefined variable: msg in
C:\xampp\htdocs\cart\shoppingcart.php on line 63
Notice: Undefined index: command in C:\wamp\www\shopping cart_rtmh\shoppingcart.php on line 66

How do i change this? I tried the "error_reporting = E_ALL & ~E_NOTICE" but hte message still exists.. please help. need this for our final school project.

Submitted bysushil shrestha (not verified)on Mon, 03/10/2014 - 13:20

i downloaded all the files. After importing sql file to mysql. All tables are imported sucessfully but it doesnot contain any fields..what to do.. plz help.

Submitted bypro (not verified)on Mon, 03/17/2014 - 16:30

Thanx alot 4 the code, but the problem am having is dat the the image is not display (product image, name and description)what do i do, plz help me.

Submitted byrichard neo (not verified)on Wed, 08/06/2014 - 22:11

i have downloaded and done everything, but it mentioned some Apache permission problems

Submitted bysimona (not verified)on Fri, 08/08/2014 - 18:38

Hi, "Update Crat" not working. I give "There are no items in your shopping cart!".
Thank you very much.

Submitted bysvn (not verified)on Wed, 10/14/2015 - 14:04

sir how to add multiple size in same product in this cart..please help...

Submitted bysylvester (not verified)on Fri, 03/10/2017 - 01:09

How to display 'Item is already in the cart' ?? please show me how to do that. thank u :)

Submitted byCTG (not verified)on Sun, 08/20/2017 - 09:34

This shopping cart does everything I need, however, it uses MySQL extensions. It would be awesome if this shopping cart was updated to MySQLi.

Thank you for posting it.

Submitted byNewbiegelo (not verified)on Tue, 10/03/2017 - 15:47

include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid']; //-- original code
//echo $_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}

?>

Notice: Undefined index: command in C:\wamp64\www\WSMshop\products.php on line 5

Promised!, i tried my best to fix this but i cant T_T. someone can lend a hand.
i already in to the link that admin tells. but i got nothing, Sorry for being not knowledgble of this, but i want to learn.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.