Can even check if this PL will work with a MYSQL server with a data base called MAP and a table called MAPtable???Thnx in advance
- Code: Select all
#!/usr/bin/perl -Tw
use strict;
$| = 1;
use CGI::Carp "fatalsToBrowser";
use CGI ":all";
use DBI;
my $serverName = "localhost";
my $serverPort = "3306";
my $serverUser = "admin";
my $serverPass = "123";
my $serverDb = "MAP";
my $serverTabl = "MAPtable";
print
header,
start_html("SQL Guestbook"),
h1("Add and Read Guestbook Entries");
if(my $error = check_form()) {
show_form($error);
print end_html;
} else {
if(my $error = insert_entry()) {
show_form($error);
} else {
show_entries();
}
print end_html;
}
sub show_form {
my $error = shift;
print hr;
if($error) { print $error, hr; }
print
start_form,
table(map
Tr(td($_->[0]), td(textfield($_->[1],"",undef,60))),
["Name", "name"],
["Grade", "grade"],
["MAP Reading", "mapreadingl"],
["MAP math", "mapmath"],
),
submit,
end_form,
hr;
}
sub check_form() {
return "You didn't enter anything..." unless param();
return "Please enter a name" unless param("name");
return "Please enter Grade Level" unless param("grade");
return;
}
sub insert_entry {
my ($dbh, $success,$name, $grade, $mapreading, $mapmath, $time);
$dbh = DBI->connect("DBI:mysql:database=$server Db;host=$serverName;port=$serverPort",$serverUser,$serverPass);
$name = param("name");
$grade = param("grade");
$mapreading = param("MAP Reading");
$mapmath = param("MAP Math");
$time = time;
$success = $dbh->do("INSERT INTO
$serverTabl(name,grade,mapreading,mapmath,time)
VALUES(?,?,?,?,?,?)", undef, $name, $grade, $mapreading, $mapmath, $time);
$dbh->disconnect;
if($success != 1) {
return "Sorry, the database was unable to add your entry.
Please try again later.";
} else {
return;
}
}
sub show_entries {
my ($dbh, $sth, @row);
$dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
$sth = $dbh->prepare("SELECT name,grade,mapreading,mapmath,time
FROM $serverTabl ORDER BY time");
$sth->execute;
print "Existing Entries",hr;
while(@row = $sth->fetchrow_array) {
$row[5] = scalar(localtime($row[5]));
print "Name: ", $row[0], br;
print "Grade: ", $row[1], br;
print "MAP Reading: ", $row[2], br;
print "MAPMath: ", $row[3], hr;
}
$sth->finish;
$dbh->disconnect;
}
