Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A problem with a mock shopping cart

Status
Not open for further replies.

noam1

Technical User
May 28, 2009
1
0
0
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" 
	 "[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>
 
It's not going into the loop because $id doesn't have a value.

Either you're not sending it one, or LoadParameters() isn't picking up the id that you are sending. I note that, when you write your form, your id field is called "ID", but when you retrieve it you're looking for a parameter called "id". I can't tell what name you're using to test it - but they need to be all the same including upper/lower case choices. Try changing to:
Code:
sub LoadParameters 
{ 
    #load parameter 
    my($id) = [red]$query->[/red]param("[red]ID[/red]");
 
    return $id; 
}

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top