in reply to Re: Re: Re: No results from script
in thread No results from script

Hi Edoc,

I tried your suggestion. It appears to me that I need to have two subroutines: one for the get_customer and one for printing the results.

I modified my code and I'm still not having luck. Can you take a look?

Thanks.
my $customer_id; $customer_id = param("customer_id"); print header(), start_html (-title => "Order Tracking System", -bgcolo +r => "white"); my $choice = lc (param ("choice")); if ($choice eq "submit") # customer submitted order number { get_customer (); print_results (); } else { print p (escapeHTML ("Logic error, unknown choice: $choice")); } print end_html(); sub get_customer { my ($dbh, $customer_id) = @_; $dbh = WebDB::connect (); my $sth = $dbh->prepare ("SELECT * FROM Shipments WHERE customer_id = +?"); $sth->execute($customer_id); while (my $track_ref = $sth->fetchrow_hashref ()){ push(@customer, $track_ref); } return(\@customer); } sub print_results { my $customer = get_customer($customer_id); print table ({-border => 0}, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"silver", -st +yle=>"font-family: verdana; font-size: 10pt;"}, th ({-width=>"50"},("Customer ID")), th ({-width=>"50"},("Carrier")), th ({-width=>"90"},("Tracking Number")) + )); foreach my $row (@$customer) { print table ({-border => 0}, Tr ({-valign=>"center", -style=>"font-family: verdana; font-size: +10pt;"}, td ($row->{customer_id}), td ($row->{carrier_id}), td ($row->{tracking_number}) )); } print start_form (-action => "http://wwwapps.ups.com/tracking/tracking +.cgi", -method => "POST"), table ( Tr ( td ("UPS Tracking Number:"), td (textfield (-name => "tracknum")) ), ), submit (-name => "choice", -value => "Track"), end_form (); }

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: No results from script
by edoc (Chaplain) on Jun 04, 2003 at 16:00 UTC

    cool.. In get_customer() your customer id is being put into $dbh. As you assign directly to $dbh you don't need it in that first line, so..

    change: sub get_customer { my ($dbh, $customer_id) = @_; $dbh = WebDB::connect (); ... to: sub get_customer { my ($customer_id) = @_; my $dbh = WebDB::connect ();

    Now you really want to keep the subs separate, so instead of calling get_customer from print_results we get_customer, then send the customer details to print results..

    if ($choice eq "submit") # customer submitted order number { get_customer (); print_results (); } else ... becomes: if ($choice eq "submit") # customer submitted order number { my $cutomer = get_customer($customer_id); print_results($customer); } else ...

    Then we change print_results to accept the customer details as an argument

    sub print_results { my $customer = get_customer($customer_id); ... becomes: sub print_results { my $customer = @_;

    cheers,

    J

      Hi,

      Well, I made the modifications. When I run the script, I'm receiving compilation errors. Unfortunately, I'm not able to see these errors on the server.

      Here's the modified code. Maybe you'll be able to spot something.

      Thanks for your help.
      #!/usr/bin/perl -Tw use CGI qw(:standard); use strict; use lib qw(/export/ext2/www/bartellmachinery/library); use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard escapeHTML); use WebDB; my $customer_id; $customer_id = param("customer_id"); print header(), start_html (-title => "Order Tracking System", -bgcolo +r => "white"); my $choice = lc (param ("choice")); if ($choice eq "submit") # customer submitted order number { my $customer = get_customer($customer_id); print_results ($customer); } else { print p (escapeHTML ("Logic error, unknown choice: $choice")); } print end_html(); sub get_customer { my ($customer_id) = @_; my $dbh = WebDB::connect (); my $sth = $dbh->prepare ("SELECT * FROM Shipments WHERE customer_id = +?"); $sth->execute($customer_id); while (my $track_ref = $sth->fetchrow_hashref ()){ push(@customer, $track_ref); } return(\@customer); } sub print_results { my $customer = @_; print table ({-border => 0}, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"silver", -st +yle=>"font-family: verdana; font-size: 10pt;"}, th ({-width=>"50"},("Customer ID")), th ({-width=>"50"},("Carrier")), th ({-width=>"90"},("Tracking Number")) + )); foreach my $row (@$customer) { print table ({-border => 0}, Tr ({-valign=>"center", -style=>"font-family: verdana; font-size: +10pt;"}, td ($row->{customer_id}), td ($row->{carrier_id}), td ($row->{tracking_number}) )); } print start_form (-action => "http://wwwapps.ups.com/tracking/tracking +.cgi", -method => "POST"), table ( Tr ( td ("UPS Tracking Number:"), td (textfield (-name => "tracknum")) ), ), submit (-name => "choice", -value => "Track"), end_form (); }
        sub get_customer { my ($customer_id) = @_; my $dbh = WebDB::connect (); my $sth = $dbh->prepare ("SELECT * FROM Shipments WHERE customer_id + = ?"); $sth->execute($customer_id); my @customer; ### <-- this was missing while (my $track_ref = $sth->fetchrow_hashref ()){ push(@customer, $track_ref); } return(\@customer); }

        first line of print_results should be:

        sub print_results { my ($customer) = @_; print table ({-border => 0},

        $customer was being assigned with the number of elements in @_...

        cheers,

        J