Chapter 8 - Shopping Cart
There are two main parts to this
chapter: 1) Entering the inventory data
into the database 2) the shopping cart.
PART 1
Entering the inventory data into the database.
This is actually in the second part of the
chapter, pages 217-234. I like to do this first
so my database will be populated when I
do the shopping cart part.
ISSUE # 1 - Browsers
Use Internet Explorer for this part.
Firefox will not work.
This was first discovered in the post
Chapter 8, shop
All this part of the code does is enter
inventory data into the database. It
will only be used by administrators of
the web site, not visitors (customers), so
I guess you could get away with imposing a
requirement that your web site administrators
use IE only. Still, it would
be much better if it could be done from any browser.
ISSUE # 2 inventory_process.php, syntax error in query statement
This issue was first addressed in the post
Shopping Cart Code incomplete
In inventory_process.php, near line 127, in the section under
PHP Code:
else if (isset($_GET['save_item']))
there is a syntax error in the $query statement
PHP Code:
.....
'( "%s", %02f, "%s" %d)',
.....
The format description is not
correct. It is missing a format descriptor for
the ITEM_DESCRIPTION field,
and it is missing a comma between the last two fields.
it should be
PHP Code:
'( "%s", "%s", %02f, "%s", %d)',
The line should look like this (Mine is slightly different, I don't
use the Wrox prefix).
PHP Code:
$query = sprintf('INSERT INTO SHOP_INVENTORY (ITEM_NAME, ' .
'ITEM_DESCRIPTION, PRICE, ITEM_IMAGE, CATEGORY_ID) VALUES ' .
'( "%s", "%s", %02f, "%s", %d)',
mysql_real_escape_string($_POST['name'], $GLOBALS['DB']),
mysql_real_escape_string($_POST['description'], $GLOBALS['DB']),
$_POST['price'],
mysql_real_escape_string($_POST['image'], $GLOBALS['DB']),
$_POST['cat_id']);
The book is also not correct, but it is not correct
in a different way.
The book does not have the format specifier
for the ITEM_IMAGE. It also does not have the
value for the ITEM_IMAGE. It appears ITEM_IMAGE
was left out altogether.
This would be on page 223, about lines 7-15,
which is the $query statement.
ISSUE # 3 - inventory_process.php, syntax error in another query statement
In that same section, near line 150, right below
PHP Code:
// update an existing record
the format specifier for price is not correct.
PHP Code:
'PRICE = %02d, ITEM_IMAGE = "%s", CATEGORY_ID = %d '
It should be %02f, not %02d
PHP Code:
'PRICE = %02f, ITEM_IMAGE = "%s", CATEGORY_ID = %d ' .
ISSUE # 4 - debugging, be careful with print statements to the browser
When debugging and troubleshooting be careful to avoid
making print statements to the browser in file
inventory_process.php. inventory_process.php is processing the AJAX request,
and putting any print statements in this file can cause
problems such as leaving fields blank that should be filled,
getting wrong entries in text fields, and other things.
It is best to write your debugging statement to an
error log file instead.
ISSUE # 5 - security around inventory.hmtl
You need to make sure people cannot get to
inventory.html from typing it in on the browser.
You could implement some security checks around
inventory.html
*****
After I did these things, the inventory part worked for me.
PART 2
The shopping cart.
This part is the first part of the chapter and
goes from page 195 to page 217. I do this part
after I have completed part 1 above, that way
I have a populated database to work with.
ISSUE # 1 - Browsers
For this shopping cart part, it seems to work with
both IE and Firefox.
ISSUE # 2 - ShoppingCart.php
ShoppingCart.php, in the download code, about line 21,
should be "break", not "brake".
PHP Code:
case 'contents':
return $this->items;
break;
This is not correct in the book either, page 199, line 2
ISSUE # 3 - closing <form> and <select> in cart.php
In another post
Shopping Cart Code incomplete depishedesign discovered
that <form> and <select> are not closed. That was a good find. Now, I actually ran the code with the tags not
closed and the code actually ran. Sometimes HTML lets you get
away with things like that, however, it is best to close
those tags.
ISSUE # 4 - Missing file - checkout.php
cart.php refers a file - checkout.php.
This file is missing. It is missing in the download code.
It is missing in the book. It is not described in
the book.
Here is the reference to it in cart.php, about line 158
PHP Code:
// display link to checkout
echo '<p><a href="checkout.php">';
echo '<img src="img/checkout.gif" alt="Proceed to Checkout"/></a></p>';
At the top of page 204, in the text the author says
"Depending on your checkout procedure...". This seems
to imply it is up to us to implement our own
checkout procedures.
I guess you could temporarily stub it out by creating
your own checkout.php that just puts
out a message or something.
*****
Other than the missing checkout.php file, the
shopping cart was working for me.
*********************
MY CONFIGURATION
I used the download code for this which
I downloaded in May 2010
browsers:
Internet Explorer 8.0
Firefox 3.0.19
> php -version
PHP 5.2.12 (cli) (built: Mar 18 2010 23:47:09)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
>
Unix:
FreeBSD 4.10-RELEASE FreeBSD 4.10-RELEASE #10: Mon Jul 30 13:42:38 MDT 2007 i386
MySQL server - 4.1.22
******************************
DUPLICATE FILE NAME ISSUE
Some of the file names used in chapter 8 are the
same file names as existing files from previous chapters.
This can result in some existing files being overwritten.
Here is a list of the files I noticed.
The
js/ajax.
js
This file was used in chapter 7.
The content is the same so no damage is done.
css/styles.css
This is different than the styles file used in chap3
and chap1.
I have named mine styles8.css
lib/common.php - unchanged
lib/db.php - unchanged