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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI program and error

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
0
0
US
I have written the following program that I access through another program. However, it give me the following error.

The server encountered an internal error or misconfiguration and was unable to complete your request.

#!/usr/bin/perl

print "Content-type:text/html\n\n";

use CGI;
$query = new CGI(param);
$d ="\$";

my $item1 = param("item");
my $price1 = param("price");
my $city1 = param("city");

$yes='no';
$p1='0';

open(INF, "<lawn.out") or dienice("Couldn't open auto.out for reading: $! \n");
@kites = <INF>;
close (INF);


foreach $i (@kites) {
chomp ($i);
($count,$name,$email,$address,$city,$state,$zipcode,$phone,$time,$pm,$item,$price,$body,$thismon,$mday,$year)=split(/\|/, $i);

if(lc($city1) eq lc($city)) {
if($price <= $price) {
if(lc($item) eq lc($item1)) {



print "<table width=\"100%\">\n";
print "<tr bgcolor=\"red\"><th>Item</th><th>Price</th><th>E-mail</th>\n";
print "<th>Telephone</th><th>Time-to-Call</th></tr>\n";

print "<td align=\"left\" width=\"25%\">$item</td><td align=\"left\" width=\"5%\"><font color=\"blue\"> $d$price </font></td>\n";
print "<td align=\"left\" width=\"20%\"><a href=\"mailto: $email\" >$email </a></td><td align=\"left\" width=\"15%\">$phone</td><td align=\"left\" width=\"15%\"> $time</td>\n";
print "<tr align=\"left\" valign=\"top\" bgcolor=\"#FF99FF\"> <td align=\"left\" colspan=\"7\" >$body</td>\n";
print "<td align=\"left\" colspan=\"2\"><font color=\"blue\">CODE#$phone $thismonth-$mday-$year</font></td></tr></table>\n";

}
}
}
}

# Wrap this code is an eval block in order to trap error


chomp $@;

if($@) { print "ERROR: $@<BR>\n"; }


print <<EndHTML;

sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
 
Can you give us the output of your error log? That will have more details.

Also, instead of

Code:
print "Content-type:text/html\n\n";

use CGI;
$query = new CGI(param);

Try

Code:
use CGI ;
my $query = new CGI ;
print $query->header;

That will get you a proper http header out.
 
The problem is that the script can't find a matching EndHTML!

Code:
print <<EndHTML;

sub dienice {
   my($msg) = @_;
   print "<h2>Error</h2>\n";
   print $msg;
[COLOR=red]EndHTML[/color]
   exit;
}

If you run your code thru the perl syntax checker, you'll save yourself a lot of time. To do this, at the perl command line type perl -c followed by your script name, and path, if necessary.

There's always a better way. The fun is trying to find it!
 
I added both statements. Now, it gives me the following:

Name "main::count" used only once: possible typo at lawnsearch2.cgi line 25. Name "main::p1" used only once: possible typo at lawnsearch2.cgi line 16. Name "main::zipcode" used only once: possible typo at lawnsearch2.cgi line 25. Name "main::name" used only once: possible typo at lawnsearch2.cgi line 25. Name "main::state" used only once: possible typo at lawnsearch2.cgi line 25. Name "main::yes" used only once: possible typo at lawnsearch2.cgi line 15. Name "main::address" used only once: possible typo at lawnsearch2.cgi line 25. Name "main::pm" used only once: possible typo at lawnsearch2.cgi line 25. Name "main::thismon" used only once: possible typo at lawnsearch2.cgi line 26. Name "main::thismonth" used only once: possible typo at lawnsearch2.cgi line 40. Undefined subroutine &main::param called at lawnsearch2.cgi line 11. Content-type:text/html Content-Type: text/html; charset=ISO-8859-1 could not fire up lawnsearch2.cgi
 
In this piece of code:
Code:
[COLOR=red]$yes='no';
$p1='0';[/color]

open(INF, "<lawn.out") or dienice("Couldn't open auto.out for reading: $! \n");
@kites = <INF>;
close (INF);


foreach $i (@kites) {
  chomp ($i);
([COLOR=red]$count,$name,[/color]$email,[COLOR=red]$address,$city,$state,$zipcode,[/color]$phone,$time,[COLOR=red]$pm,[/color]$item,$price,$body,[COLOR=red]$thismon,[/color]$mday,$year)=split(/\|/, $i);

Everything in RED are variables you're declaring but never using. (I think you've got a real type in this line: $thismonth-$mday-$year</font></td></tr></table>\n";)


Also, you're not correctly using the param() function. Instead of this:

Code:
use CGI;
$query = new CGI(param);

use this:

Code:
my $query = new CGI;

Also, instead of this:
Code:
print "Content-type:text/html\n\n";

use this:
Code:
print $query->header();

One last thing to make your code easier to read and write. Change this:
Code:
print "<table width=\"100%\">\n";
print "<tr bgcolor=\"red\"><th>Item</th><th>Price</th><th>E-mail</th>\n";
print "<th>Telephone</th><th>Time-to-Call</th></tr>\n";

print "<td align=\"left\" width=\"25%\">$item</td><td align=\"left\" width=\"5%\"><font color=\"blue\"> $d$price </font></td>\n";
print "<td align=\"left\" width=\"20%\"><a href=\"mailto: $email\" >$email </a></td><td align=\"left\"  width=\"15%\">$phone</td><td align=\"left\"  width=\"15%\"> $time</td>\n";
print "<tr align=\"left\" valign=\"top\" bgcolor=\"#FF99FF\"> <td  align=\"left\" colspan=\"7\" >$body</td>\n";
print "<td align=\"left\" colspan=\"2\"><font color=\"blue\">CODE#$phone $thismonth-$mday-$year</font></td></tr></table>\n";

to this:

Code:
print<<EOF:
<table width="100%">
<tr bgcolor="red"><th>Item</th><th>Price</th><th>E-mail</th><th>Telephone</th><th>Time-to-Call</th></tr>
<td align="left" width="25%">$item</td><td align="left" width="5%"><font color="blue"> $d$price </font></td>
<td align="left" width="20%"><a href="mailto: $email" >$email </a></td><td align="left"  width="15%">$phone</td><td align="left" width="15%"> $time</td></tr>
<tr align="left" valign="top" bgcolor="#FF99FF"><td  align="left" colspan="7" >$body</td><td align="left" colspan="2"><font color="blue">CODE#$phone $thismonth-$mday-$year</font></td></tr></table>
EOF

You don't have all the print statements, you don't have to escape the quotation marks, and you don't have to put a newline (\n) at the end of each line.

There's always a better way. The fun is trying to find it!
 

Also too, this line is a typo.

if($price <= $price) {

if price is less-than or equal to self?

Unless of course you meant this line to always be true.
 
Good eye, crackn101. I missed that completely. For samesale, this is, in fact, a typo but it won't cause the errors you're getting. The script won't return rusults when you expect them.

There's always a better way. The fun is trying to find it!
 
the bit in the [red]EndHTML[/red] doesn't make much sense?

... you surely don't want the sub dienice { ... } being printed with the HTML page?


Kind Regards
Duncan
 
this could be some HTML output produced by your script

Code:
Content-type:text/html


sub dienice {
   my() = ;
   print "<h2>Error</h2>
";
   print ;
   exit;
}

this is only happening as you are in a print HERE


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top