CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
A problem with a mock shopping cart


New User

May 28, 2009, 5:14 AM

Post #1 of 4 (596 views)
A problem with a mock shopping cart Can't Post


So a friend has helped me in setting up a simple shopping cart using an Access database accessed through Windows ODBC. The page when loaded does not display any of the products on the page. The code must not be going into the if loop where the table with the products in is created, but I just can't figure out why... If anyone has any suggestions what could be causing the problem I would be really grateful.

There are no errors in the error.log.

This is

use CGI qw(param);
use Win32::ODBC;
## program -- demo

# create a new CGI query object
$query = new CGI;

# create a new datasource object
$datasource = new Win32::ODBC("Toyshop")
or die Win32::ODBC::Error();

sub LoadParameters
#load parameter
my($id) = param("id");

return $id;

sub RequestProduct
my($id) = @_;

my($quantity) = 1;
my($description) = 'Description';
my($price) = 0.00;

# check for a valid parameter
if($id >= 1 && $id <= 9999)
# checks if the Sql sent to the datasource returns anything
$selectProduct = 'SELECT * FROM Product WHERE ID = ' . $id;
# if the Sql returns nothing then an error will be printed
print "SQL Error: " . $datasource->Error() . "\n";

# the datasource connection is closed

# exit
# while there is still a row to fetch from the datasource
# put data from the datasource in a hash
my(%data) = $datasource->DataHash();

# print the results of the datasource
print "<table>",

$id = $data{'ID'};
$description = $data{'Description'};
$price = $data{'Price'};
print $query->br;

# the datasource connection is closed

return $id, $quantity, $description, $price;

sub StartHTML
#$cart = $query->cookie(-name=>'CART',-value=>($cart));

# print an HTML header
print $query->header,

# start the HTML

sub PrintForm
my($id, $quantity, $description, $price) = @_;

my($method) = 'POST';
my($actionBuy) = '';

print $query->startform($method,$actionBuy),
$query->submit(-value=>'Add to Cart'),

sub EndHTML
print $query->end_html;

sub Main


The HTML that comes back is here:

<!DOCTYPE html 
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html xmlns="" lang="en-US" xml:lang="en-US">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<h3>Product</h3><form method="post" action="" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="ID" value="" /><input type="hidden" name="Quantity" value="1" /><input type="hidden" name="Description" value="Description" /><input type="hidden" name="Price" value="0" /><input type="submit" name=".submit" value="Add to Cart" /></form><br />

Veteran / Moderator

May 28, 2009, 6:36 AM

Post #2 of 4 (592 views)
Re: [noam] A problem with a mock shopping cart [In reply to] Can't Post

You never executed the subroutines that query the database.

Oops, I missed the call to RequestProduct() when calling PrintForm().

(This post was edited by FishMonger on May 28, 2009, 6:39 AM)

Veteran / Moderator

May 28, 2009, 6:45 AM

Post #3 of 4 (587 views)
Re: [noam] A problem with a mock shopping cart [In reply to] Can't Post

Start by adding the strict and warnings pragmas and fix the problems that they pint out. Then enable debug when creating the ODBC object and add some print statements at the key points to see if the vars hold the expected values.

(This post was edited by FishMonger on May 28, 2009, 6:45 AM)

New User

May 29, 2009, 6:30 AM

Post #4 of 4 (568 views)
Re: [FishMonger] A problem with a mock shopping cart [In reply to] Can't Post

OK no worries now, I have fixed it! Thank you :D


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives