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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



noam
New User

May 28, 2009, 5:14 AM

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

OK,

So a friend has helped me in setting up a simple shopping cart using an Access database accessed through Windows ODBC. The product.pl 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 product.pl:


Code
 
#!c:/perl/bin/perl.exe
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($datasource->Sql($selectProduct))
{
# if the Sql returns nothing then an error will be printed
print "SQL Error: " . $datasource->Error() . "\n";

# the datasource connection is closed
$datasource->Close();

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

# print the results of the datasource
print "<table>",
"<tr><td><b>ID:</b></td><td>$data{'ID'}</td></tr>",
"<tr><td><b>Description:</b></td><td>$data{'Description'}</td></tr>",
"<tr><td><b>Details:</b></td><td>$data{'Details'}</td></tr>",
"<tr><td><b>Price:</b></td><td>$data{'Price'}</td></tr>",
"<tr><td><b>Image:</b></td><td>$data{'Image'}",
"</tr></table>";

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

# the datasource connection is closed
$datasource->Close();
}
}

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

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

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

# start the HTML
$query->start_html(-title=>'Product',-dtd=>'1'),
$query->h3('Product');
}

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

my($method) = 'POST';
my($actionBuy) = 'cart.pl';

print $query->startform($method,$actionBuy),
$query->hidden(-name=>'ID',-value=>$id),
$query->hidden(-name=>'Quantity',-value=>$quantity),
$query->hidden(-name=>'Description',-value=>$description),
$query->hidden(-name=>'Price',-value=>$price),
$query->submit(-value=>'Add to Cart'),
$query->endform,
$query->br;
}

sub EndHTML
{
print $query->end_html;
}

sub Main
{
StartHTML();
PrintForm(RequestProduct(LoadParameters()));
EndHTML();
}

Main();


The HTML that comes back is here:


Code
<!DOCTYPE html 
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Product</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Product</h3><form method="post" action="cart.pl" 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 />
</body>
</html>



FishMonger
Veteran / Moderator

May 28, 2009, 6:36 AM

Post #2 of 4 (534 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.

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


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


FishMonger
Veteran / Moderator

May 28, 2009, 6:45 AM

Post #3 of 4 (529 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)


noam
New User

May 29, 2009, 6:30 AM

Post #4 of 4 (510 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