×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

A problem with a mock shopping cart

A problem with a mock shopping cart

A problem with a mock shopping cart

(OP)
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>

RE: A problem with a mock shopping cart

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) = $query->param("ID");
 
    return $id;
}

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close