I am trying to make a database connection with a cgi script from a Web
page.

I have installed several DBI modules for Perl to recognize Postgres:
PgSQL.pm, Renderer.pm, DBI.pm are the ones that matter.  I did not
notice any errors on install, and ran a couple of test perl scripts
which outputted SQL statements as predicted.

I am using a script which is working on another Solaris server, adapting
it to make it work on my Solaris server.  The changes are:
- deleted "use Postgres;" and replaced with "use PgSQL::Cursor;" and
"use DBIx::Renderer ':all';" since that is the syntax that my DBI
modules have.  
- omitted the "require xxx.lib" because the script would always fail on
compilation at the "require xxx.lib" line.  
- copied the parts of the library file that I needed to the cgi script.


Now the error that I get is: "xxx.cgi did not produce a valid header
(name without value:got line "variable $i" is not imported at xxx.cgi
line 65.") I need to declare the subroutine "row" in which the error is
found to do a "fetchrow" later in the script, and I can't see an error
in the subroutine itself.  Help!

I have copied the script below.  I need to match keyboard input to a
field in the database and output the fields ("aname" and "meaning") for
an acronym lookup.  
Thanks for any help and/or references you can provide.

Christine

#!/usr/local/bin/perl

use warnings;
use strict;
use DBIx::Renderer ':all';

use CGI qw(:standard);
use PgSQL::Cursor;

#General variables

$dbh = db_connect("mydb") or die "Error: $Postgres::error";

#Sub for getting cgi inputs
sub parse_form {
# --------------------------------------------------------
# Parses the form input and returns a hash with all the name
# value pairs. Removes SSI and any field with "---" as a value 
# (as this denotes an empty SELECT field.

        my (@pairs, %in);
        my ($buffer, $pair, $name, $value);     

        if ($ENV{'REQUEST_METHOD'} eq 'GET') {
                @pairs = split(/&/, $ENV{'QUERY_STRING'});
        }
        elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
                read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
                @pairs = split(/&/, $buffer);
        }
        else {
        }
        PAIR: foreach $pair (@pairs) {
                ($name, $value) = split(/=/, $pair);
                 
                $name =~ tr/+/ /;
                $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

                $value =~ tr/+/ /;
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

                #$value =~ tr/a-z/A-Z/;

                $value =~ s/<!--(.|\n)*-->//g;                    
                # Remove SSI.
                if ($value eq "---") { next PAIR; }               

                # This is used as a default choice for select lists 
                # and is ignored.
                (exists $in{$name}) ?
                        ($in{$name} .= "~~$value") :          
                # If we have multiple select, then we tack on
                        ($in{$name}  = $value);                           
                # using the ~~ as a seperator.
        }
        return %in;
}

sub row 
{

        $r = shift(@_);
        @r = @_;
        $nfields = $r->nfields;
        for ($i=0; $i<$nfields; $i++) {
                $fname = $r->fname($i);
                $r{$fname} = $r[$i]; 
        }
        return %r;
}

local(%in) = &parse_form;

print <<"XXXX";

<HTML>
<HEAD>
<TITLE>Acronym Lookup</TITLE>
</HEAD>

<body>

<!--
<p align=center>lookup * $in{'lookup'}</p>
-->

XXXX
print "<h2 align=center>Acronym Lookup</h2>";

        
##########################################################################
        if ($in{'lookup'} ne "")
         {&display_acronym }
        
##########################################################################       
sub display_acronym
{
print "<table align=center>";

$caps = uc($in{'lookup'});
$sql = "select aname, meaning from acronyms where aname = '$caps'";
#$sql = "select aname, meaning from acronyms where aname =
'$in{'lookup'}'";

#print "<p align=center>*$sql</p>";
$found_name = 0;
$result = $dbh->execute($sql);
while (@row = $result->fetchrow() )
        {
        %row = &row($result, @row);
        $found_name = 1;

print "<tr><td><b> $row{'aname'}:</b></font></td><td>
$row{'meaning'}</font></td></tr>";
    }   
        if ($found_name == 1)
                {
                print "</table>";
                }

else {
print "<p align=center>Acronym <b> $in{'lookup'} </b> not found - Go
back and Re-enter</p>";                                                 
         }
}
#############################################################################

print <<"XXXX";

<p align=center><A HREF=\"lookup.htm"><img src="prev.gif"
border=0></A></p>

</BODY>
</HTML>

XXXX

Reply via email to