noam1
Technical User
- May 28, 2009
- 1
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:
The HTML that comes back is here:
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"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] 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-[URL unfurl="true"]www-form-urlencoded">[/URL]
<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>