Simple PHP MySQL Shopping cart – PART 1

Hello everyone, this is my first tutorial ever written, a very simple and basic shopping cart script made in PHP and MySQL. This tutorial I have made for understanding the basics of making a shopping cart in PHP and MySQL.

First of all we need to make a database with some information in it. The MYSQL query is as follow:

--
-- Table structure for table `cart`
--
 
CREATE TABLE `cart` (
  `cartId` int(11) NOT NULL AUTO_INCREMENT,
  `sessionId` varchar(50) DEFAULT NULL,
  `itemId` int(11) DEFAULT NULL,
  `qty` int(11) DEFAULT NULL,
  PRIMARY KEY (`cartId`),
  UNIQUE KEY `id` (`cartId`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
 
--
-- Dumping data for table `cart`
--
 
INSERT INTO `cart` VALUES(11, '6066bf574942638990435126fb5154c1', 1, 10);
INSERT INTO `cart` VALUES(12, '6066bf574942638990435126fb5154c1', 2, 26);
INSERT INTO `cart` VALUES(13, '16fea7056e1770e1fe37e5b13ffc8cf6', 1, 19);
INSERT INTO `cart` VALUES(14, '16fea7056e1770e1fe37e5b13ffc8cf6', 2, 20);
 
-- --------------------------------------------------------
 
--
-- Table structure for table `items`
--
 
CREATE TABLE `items` (
  `itemId` int(11) NOT NULL AUTO_INCREMENT,
  `itemName` varchar(50) DEFAULT NULL,
  `itemDesc` varchar(250) DEFAULT NULL,
  `itemPrice` decimal(4,2) DEFAULT NULL,
  PRIMARY KEY (`itemId`),
  UNIQUE KEY `id` (`itemId`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
 
--
-- Dumping data for table `items`
--
 
INSERT INTO `items` VALUES(1, 'Skeelers', 'Like ice skating but different', 9.95);
INSERT INTO `items` VALUES(2, 'Pencil', 'To write stuff', 1.45);

As you can see I have put some products in the items table, we will show these items in list.php where we find all the products and add them to the shopping cart.

sqlitems Simple PHP MySQL Shopping cart   PART 1

Now we begin to start a session() and a database connection. We call the this file db.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
 
session_start();
 
//the database variables, fill in your own if you are not using mamp
$host = "localhost";
$user = "root";
$password = "root";
$dbname = "simplephpshoppingcart";
 
$db = mysql_connect($host,$user,$password) or die("Database error");
mysql_select_db($dbname, $db);
 
?>

So far it is pretty simple. In part 2 I will show the list.php and cart.php to complete the very basic shop. Which we will improve by time and my hopefully my tutorial skills will be better by that time.

Go to Simple PHP MySQL Shopping Cart – Part 2.

Related Posts

Popular Posts


6 Responses

12.01.09

thanx for the lesson really helpful
but i have this error
Warning: Missing argument 2 for UpdateProduct(), called in C:\AppServ\www\s\cart.php on line 36 and defined in C:\AppServ\www\s\cart.php on line 41

Warning: Cannot modify header information – headers already sent by (output started at C:\AppServ\www\s\cart.php:41) in C:\AppServ\www\s\cart.php on line 44
what to do ???

12.01.09

Session_start(); should be above the php opening tag

12.01.09

Son of a gun, this is so hlefupl!

Hello! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one? Thanks a lot!

12.01.09

Hi there, You’ve performed an incredible job. I’ll certainly digg it and in my view suggest to my friends. I’m confident they’ll be benefited from this website.

12.01.09

Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but instead of that, this is fantastic blog. An excellent read. I’ll certainly be back.

Leave Your Response

* Name, Email, Comment are Required

Categories

Archives