Pasted below is my Perl script to generate a data entry form on a
webpage, process the data when Submit is clicked, and insert the data
into my MySQL database.
Now, I'm trying to do the opposite, and display query results from
same database to a webpage, but my Perl script isn't working. I need a
simple Perl script to "SELECT * FROM STAFF" and display those results
on a webpage.
"insert.pl" script: (currently functional)
#!/usr/bin/perl
use DBI;
use CGI;
my $q = new CGI;
# Definitions
my $table = "STAFF";
my $db = "test";
my $user = "rick";
my $password = "rick";
# end definitions
if ($q->param('name') eq "") {&printform()} else {&results()}
sub printform {
print $q->header;
print $q->start_html (-title=>'test database',
-BGCOLOR=>'black',
-TEXT=>'white');
print "<CENTER><H1>Test Database</H1></CENTER><HR>";
print $q->startform;
print "Name: ", $q->textfield(-name => name,
-size => 16), "<BR>";
print "Phone: ", $q->textfield(-name => phone,
-size => 16), "<BR>";
print "Address: ", $q->textfield(-name => address,
-size => 16), "<BR>";
print "Age: ", $q->textfield(-name => age,
-size => 16), "<BR>";
print $q->submit;
print $q->endform;
print $q->end_html;
}
sub results {
my ($name, $phone, $address, $age);
$name = $q->param('name');
$phone = $q->param('phone');
$address = $q->param('address');
$age = $q->param('age');
print $q->header;
print $q->start_html (-title=>'Database Results',
-BGCOLOR=>'black',
-TEXT=> 'white');
# Tell script we will use a MySQL database
my ($drh, $dbh);
$drh = DBI->install_driver( 'mysql' );
# Establish connection with database
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","rick","rick",
{'RaiseError'=> 1});
#Check connection
if (!$dbh) {
print "Cannot connect: $DBI::errstr<BR>";
print $q->end_html;
die;
}
#Build and do SQL statement
my $sql_name = $dbh->quote($name);
my $sql_add = $dbh->quote($address);
my $sql_ph = $dbh->quote($ph);
my $sql_age = $dbh->quote($age);
#$SQLstatement = "insert ($sql_name)";
$SQLstatement = "insert into $table (name, phone, address, age) values
($sql_name, $sql_ph, $sql_add, $sql_age)";
#print "$SQLstatement";
$dbh->do($SQLstatement);
print "$name added to database<br><br>";
print $->end_html;
}
"select.pl" Perl script (currently non-funcitonal--i.e. "Internal
Server error--'can't call method 'header' " results when accessed from
browser
I need the "select.pl" script to work. Please give working coe only,
no tutorial links or technological alternatives.
Thank you |
Clarification of Question by
omniscientbeing-ga
on
08 Nov 2002 11:27 PST
Sorry, I meant to paste the non-funcitonal code as well. Here it is:
#! /usr/bin/perl -w
# connect to MySQL, retrieve data, write HTML output
use DBI;
use CGI;
my ($dbh, $sth);
#establish connection to database
$dbh = DBI->connect("DBI:mysql:database=test;host=localhost","rick","rick",
{'RaiseError'=> 1});
#MySQL database query
$sth = $dbh->prepare ("SELECT * FROM STAFF");
$sth->execute ();
#generate HTML page with query results
print $q->header;
print $q->start_html (-title=>'test database',
-BGCOLOR=>'black',
-TEXT=>'white');
$sth->finish ();
$dbh->disconnect ();
exit (0);
|