Shopping cart with php and mysql updating quantity and saving orders

Create a new MySQL database named demo and execute the SQL code below:

-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Apr 30, 2015 at 09:23 PM
-- Server version: 5.6.17
-- PHP Version: 5.5.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `demo`
--

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

CREATE TABLE IF NOT EXISTS `product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
  `price` decimal(10,0) NOT NULL,
  `quantity` int(11) NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`id`, `name`, `price`, `quantity`, `description`) VALUES
(1, 'Name 1', '1000', 2, 'good'),
(2, 'Name 2', '2000', 3, 'good'),
(3, 'Name 3', '3000', 4, 'good');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Create PHP file named connect.php. Use mysqli_connect method connect to demo database with default account:


Username: root
Password:


Create PHP file named index.php. This file will list all product from database as below:


Id Name Price Buy
id; ?> name; ?> price; ?> Order Now

This class contains information about a product in the cart


Create new PHP file named index.php. This page lists the products in the shopping cart and allows you to delete and update quantity of items in cart


id = $product->id;
	$item->name = $product->name;
	$item->price = $product->price;
	$item->quantity = 1;
	// Check product is existing in cart
	$index = - 1;
	if (isset ( $_SESSION ['cart'] )) {
		$cart = unserialize ( serialize ( $_SESSION ['cart'] ) );
		for($i = 0; $i < count ( $cart ); $i ++)
		if ($cart [$i]->id == $_GET ['id']) {
			$index = $i;
			break;
		}
	}
	if ($index == - 1)
	$_SESSION ['cart'] [] = $item;
	else {
		$cart [$index]->quantity ++;
		$_SESSION ['cart'] = $cart;
	}
}

// Delete product in cart
if (isset ( $_GET ['index'] ) && !isset($_POST['update'])) {
	$cart = unserialize ( serialize ( $_SESSION ['cart'] ) );
	unset ( $cart [$_GET ['index']] );
	$cart = array_values ( $cart );
	$_SESSION ['cart'] = $cart;
}

// Update quantity in cart
if(isset($_POST['update'])) {
	$arrQuantity = $_POST['quantity'];

	// Check validate quantity
	$valid = 1;
	for($i=0; $iquantity = $arrQuantity[$i];
		}
		$_SESSION ['cart'] = $cart;
	}
	else
		$error = 'Quantity is InValid';
}

?>

price * $cart [$i]->quantity; ?>
Option Id Name Price Quantity Sub Total
Delete id; ?> name; ?> price; ?> price * $cart[$i]->quantity; ?>
Sum

Continue Shopping

Product List Page

Shopping cart with php and mysql updating quantity and saving orders

Cart Page

Shopping cart with php and mysql updating quantity and saving orders

I recommend you refer to the books below to learn more about the knowledge in this article:

  • Murach’s PHP and MySQL (3rd Edition)
  • Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
  • PHP and MySQL Web Development (5th Edition) (Developer’s Library)
  • Murach’s MySQL, 2nd Edition
  • MySQL (5th Edition) (Developer’s Library)
  • PHP Ajax Cookbook

How do you change the quantity on a shopping cart?

After adding an item to the shopping cart, click View Cart ---OR--- from the upper right-hand corner of the screen, click the Shopping Cart icon. To the right of the item you wish to update, type in the quantity you'd like to purchase ---OR--- use the +/- icons to add/remove items.

How to build a shopping cart in PHP and MySQL?

In this tutorial we'll be creating a shopping cart system with PHP and MySQL..
Click the Databases tab at the top..
Under Create database, type in shoppingcart in the text box..
Select utf8_general_ci as the collation (UTF-8 is the default encoding in HTML5).
Click Create..

How to add items to shopping cart in PHP?

php $_SESSION['cart'] = array(); if(isset($_GET['search'])){ echo ""; echo ""; echo ""; echo ""; foreach($xml->categories->category->items->product as $product){ $imageURL = $product->images->image[0]->sourceURL; $id = $product['id']; echo ""; ...

How to create a cart page in PHP?

How to Create a Shopping Cart in PHP and MySQL.
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..

Product ImageProduct NamePrice