slok has asked for the wisdom of the Perl Monks concerning the following question:

I have some problem doing conditioning after retrieving results back from a db query.
What I want to do is a "if-else" , if user is found, redirect to "app.pl" else show some error messages.
any idea what's wrong?
I get a "500 Internal Server Error" message

=====
do 'dbi-lib.pl'; use CGI ':standard'; use DBI; use strict; use vars qw($baseURL); # Declare base URL for return from this page $baseURL = "../index.html"; # print HTML header identifying document as HTML from dbi-lib.pl printHead('Logon User - Get Data'); print <<ENDOFTEXT; <H1>TDPS</h1> <br> ENDOFTEXT createform(); printclose(); # # Gather data to create HTML form ------------------------------------ +--- # sub createform { my $q = new CGI; # get values passed from form my $user = $q->param('user'); my $passwd = $q->param('passwd'); my $dbuser = 'ass2'; my $dbpasswd = 'tdps'; my $cnt; # Database DBI driver name my $dbname = "DBI:Oracle:"; # Create database handle my $dbh = DBI->connect($dbname, $dbuser, $dbpasswd); if (!$dbh) { showSQLerror("Connecting to the database"); return; } else { # set AutoCommit 1 = ON, 0 = OFF $dbh->{AutoCommit} = 1; } # Select to collect department numbers and names for pull down lis +t # on form - **ONLY** use such lists if count of rows would be smal +l # say 10 - 15 rows my $sql = "SELECT count(*) as cnt from users where USER_CODE = \'$ +user\' and PIN = \'$passwd\'"; # my $sql = "SELECT count(*) as cnt from users"; # Create a statement handle - prepare the statement my $sth = $dbh->prepare($sql); if (!$sth) { showSQLerror("Preparing SQL statement"); $dbh->disconnect; return; } # Execute the statement against the database $sth->execute; if ($DBI::errstr) { showSQLerror("Executing SQL Statment"); $sth->finish; $dbh->disconnect; return; } # Output the data fetched while (my ($cnt) = $sth->fetchrow) { if (my $cnt == 0) { print <<ENDOFTEXT; <table border=2 width=400> <tr> <td width=250><b>Count0</b></td> <td width=150>$cnt</td> </tr> ENDOFTEXT } else { print <<ENDOFTEXT; count1<p>$cnt ENDOFTEXT } print <<ENDOFTEXT; </table> ENDOFTEXT $sth->finish; $dbh->disconnect; }
=====

Edit kudra, 2002-05-07 Added READMORE

Replies are listed 'Best First'.
Re: CGI question - Header Error?
by tadman (Prior) on May 07, 2002 at 07:05 UTC
    Massive points off for using do instead of use.

    Further, I suspect that the error is in that code since printHeader is not defined in your example and the problem is likely header related.

    Check that you are actually printing a header:
    my $cgi = CGI->new(); print $cgi->header();
    An invalid header is penalized automatically with a "500" error.

    For redirection, use:
    print $cgi->redirect("some/url");
    It is particularily important to note that if your URL is prefixed with "http://" then it is a client-side redirect, where the client will actually fetch a separate Web page and the URL in the browser will change. If it is not prefixed like this, then it is a server-side redirect and the client URL will not change, but the data will. It's a "silent" redirect, if you will.
Re: CGI question
by grep (Monsignor) on May 07, 2002 at 07:08 UTC
    Whenever you get a 500 error, first step I would recommend in your debug procedure is to run it from the command line. Most times a an error will pop up right there. What you want to check for is something (like an error message) being printed before your header (speaking of which we can't see your code that is printing you headers, so I can't comment on how you are printing your headers, which could be another problem).

    Second step would be to check the log files for an error message. This might clue you into a permission problem.

    If you do those 2 steps there isn't usually a step 3 :)


    couple comments on your code:

    You are calling in all the CGI methods into your namespace (use CGI ':standard') but then you use the Object interface.

    You do a 'use vars qw($baseURL);' when (I'm guessing) you only need a lexical scoped to the main package 'my $baseURL;'



    grep
    Unix - where you can throw the manual on the keyboard and get a command
Re: CGI question
by tachyon (Chancellor) on May 07, 2002 at 15:21 UTC

    CGI Help Guide, secifically the Debugging section. Add this to your script:

    # ensure all fatals go to browser during debugging and set-up # comment this BEGIN block out on production code for security BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print