Build Shopping Cart With PHP and MySQL


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:

<?php
	$con = mysqli_connect('localhost', 'root', '', 'demo');
?>

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

<?php
	require 'connect.php';
	$result = mysqli_query($con, 'select * from product');
?>
<table cellpadding="2" cellspacing="2" border="0">
	<tr>
		<th>Id</th>
		<th>Name</th>
		<th>Price</th>
		<th>Buy</th>
	</tr>
	<?php while($product = mysqli_fetch_object($result)) { ?>
		<tr>
			<td><?php echo $product->id; ?></td>
			<td><?php echo $product->name; ?></td>
			<td><?php echo $product->price; ?></td>
			<td><a href="cart.php?id=<?php echo $product->id; ?>">Order Now</a></td>
		</tr>
	<?php } ?>
</table>

Create PHP file named item.php. This class contains information about a product in the cart

<?php
class Item{
	var $id;
	var $name;
	var $price;
	var $quantity;
}
?>




Create PHP file named cart.php. This page lists the products in the shopping cart and allows you to delete the selected product


<?php
session_start ();
require 'connect.php';
require 'item.php';
if (isset ( $_GET ['id'] )) {
	$result = mysqli_query ( $con, 'select * from product where id=' . $_GET ['id'] );
	$product = mysqli_fetch_object ( $result );
	$item = new Item ();
	$item->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'] )) {
	$cart = unserialize ( serialize ( $_SESSION ['cart'] ) );
	unset ( $cart [$_GET ['index']] );
	$cart = array_values ( $cart );
	$_SESSION ['cart'] = $cart;
}
?>
<table cellpadding="2" cellspacing="2" border="1">
	<tr>
		<th>Option</th>
		<th>Id</th>
		<th>Name</th>
		<th>Price</th>
		<th>Quantity</th>
		<th>Sub Total</th>
	</tr>
	<?php
	$cart = unserialize ( serialize ( $_SESSION ['cart'] ) );
	$s = 0;
	$index = 0;
	for($i = 0; $i < count ( $cart ); $i ++) {
		$s += $cart [$i]->price * $cart [$i]->quantity;
		?>
	<tr>
		<td><a href="cart.php?index=<?php echo $index; ?>"
			onclick="return confirm('Are you sure?')">Delete</a></td>
		<td><?php echo $cart[$i]->id; ?></td>
		<td><?php echo $cart[$i]->name; ?></td>
		<td><?php echo $cart[$i]->price; ?></td>
		<td><?php echo $cart[$i]->quantity; ?></td>
		<td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?></td>
	</tr>
	<?php
		$index ++;
	}
	?>
	<tr>
		<td colspan="5" align="right">Sum</td>
		<td align="left"><?php echo $s; ?></td>
	</tr>
</table>
<br>
<a href="index.php">Continue Shopping</a>




Product List Page

Cart Page

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