Adding an Item to a Shopper's Basket

 

Introduction.  The first thing we need to do is create the capabilities for a shopper to add something to their basket.  But, we also need to be able to handle a variety of contingencies.
  • they haven't put anything in their basket this session
    • create a basket and give them a new id_basket
    • add the selected item(s)
  • add the item to a basket already started this session identified by id_basket

All of our basic functionalities will revolve around the basket, which we will not develop until the next webpage.

The following PHP page should be called add_item.php and it should be uploaded to your firefox website.

 

<html>
<!-- add_item.php - adding an item to a shopper's basket -->

<?php
include("includes/connection_config.php");
// if they don't have a basket then we need to create one
// first we need to determine whether the shopper already has a basket during this session

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
session_start();
if (!($_SESSION['id_basket'] > 0))
{

// creating the query string
$query_string = "INSERT INTO basket VALUES ('0', 0, 0.0, 0.0, 0.0, 0.0, Now())";
// executing the SQL statement
mysql_db_query($db_name, $query_string, $link);
// obtain the id_shopper
// getting the id_basket for this autoincrement
$_SESSION['id_basket'] = mysql_insert_id();
$id_basket = $_SESSION['id_basket'];

}
// making sure that any attributes that aren't set can be inserted as blank
if (!(isset($sel_flavor)))
{

$sel_flavor = '';

}
if (!(isset($sel_size)))
{

$sel_size = '';

}
// now we need to get all the basic info on the product
// in order to insert it into the basket_item table

$query_prod_string = "SELECT * FROM products WHERE id_product = $id_product";
$result_prod_set = mysql_db_query($db_name, $query_prod_string, $link);
while ($row = mysql_fetch_array($result_prod_set))
{

$product_name = $row[product_name];
$weight = $row[weight];
$price = $row[price];

}
// insert the info selected by the shopper into the basket_item table
$query_insert_string = "INSERT INTO basket_item VALUES ('0', $id_product, $price, $weight, '$product_name', $txt_quantity, $id_basket, '$sel_flavor', '$sel_size')";
mysql_db_query($db_name, $query_insert_string, $link);

mysql_close($link);
header("location:basket.php");
?>
</body>
</html>

 

 

In ASP and SQL Server one would obtain the id_basket using a second SELECT query within the SQL stored procedure where we instantiate a new basket.  The SQL Server command looks something like the following

select idbasket = @@identity

It seems that in PHP/MySQL this is done by using a built-in PHP function rather than a MySQL command.  At least I didn't discover such a MySQL command analogous to the SQL Server SELECT.  In PHP/MySQL the function

mysql_insert_id()

is used to retrieve the auto-incremented id_basket for the most recent INSERT command.

We now give some discussion of the code.

  • we start by initializing the setting for connecting to our database
  • we check whether the shopper has already initiated a shopping basket by looking at the session variable $_SESSION['id_basket']
    • if it isn't positive
      • we know they don't have an id_basket
      • we need to insert a new record with zeroed out entries except for the date:time stamp, remember the id_basket will auto-increment
      • the id_basket is returned with the mysql_insert_id() function
  • the next set of code always executes
  • we query the products table to get the information on the product
    • the id_product is passed to this page by accessing a hidden text box in any of the product.php pages
  • we insert into the basket_item table
    • most of this product info along with
    • the quantity the shopper has ordered and
    • the id_basket
    • and any product attributes
  • we close the link
  • we redirect the shopper to the basket.php page (yet to be created) where their basket will be displayed