The following perl application will
both display a form for user entries into the
guestbook, to add new entries into
the guestbook and then display the guestbook.
#!/usr/bin/perl -Tw
use strict;
$| = 1;
use CGI::Carp "fatalsToBrowser";
use CGI ":all";
use DBI;
my $serverName = "localhost";
my $serverPort = "3306";
my $serverUser = "guestbook";
my $serverPass = "guestbook";
my $serverDb = "guestbook";
my $serverTabl = "guestbook";
sub check_form() {
return "You didn't enter anything..." unless param();
return "Please enter a name" unless param("name");
return "Please enter your e-mail address" unless param("email");
return;
}
sub insert_entry {
my ($dbh, $success, $name, $age, $email, $website,
$comments,$time);
$dbh = DBI->connect("DBI:mysql:database=$server
Db;host=$serverName;port=$serverPort",$serverUser,$serverPass);
$name = param("name");
$age = param("age");
$email = param("email");
$website = param("website");
$comments = param("comments");
$time = time;
$success = $dbh->do("INSERT INTO
$serverTabl(name,age,email,website,comments,time)
VALUES(?,?,?,?,?,?)", undef, $name, $age, $email, $website, $comments,
$time);
$dbh->disconnect;
if($success != 1) {
return "Sorry, the database was unable to add your entry.
Please try again later.";
} else {
return;
}
}