Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Oct 2, 1999

Setting Up a MySQL Based Website - Part I - Page 4

By DatabaseJournal.com Staff

by Andrew Chen

Writing the Perl Application

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";

     start_html("SQL Guestbook"),
     h1("Add and Read Guestbook Entries");
if(my $error = check_form()) {
     print end_html;
} else {
     if(my $error = insert_entry()) {
     } else {
     print end_html;

sub show_form {
     my $error = shift;
     print hr;
     if($error) { print $error, hr; }
          Tr(td($_->[0]), td(textfield($_->[1],"",undef,60))),
          ["Name", "name"],
          ["Age", "age"],
          ["E-Mail Address", "email"],
          ["Web Site Address", "website"],
          ["Comments", "comments"],

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");

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
               VALUES(?,?,?,?,?,?)", undef, $name, $age, $email, $website, $comments, $time);
     if($success != 1) {
          return "Sorry, the database was unable to add your entry.
               Please try again later.";
     } else {

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,age,email,website,comments,time
               FROM $serverTabl ORDER BY time");
     print "Existing Entries",hr;
     while(@row = $sth->fetchrow_array) {
          $row[5] = scalar(localtime($row[5]));
          print "Name: ", $row[0], br;
          print "Age: ", $row[1], br;
          print "E-Mail Address: ", $row[2], br;
          print "Web Site Address: ", $row[3], br;
          print "Comments: ", $row[4], br;
          print "Added on ", $row[5], hr;

Page 5: In Closing...

MySQL Archives

Latest Forum Threads
MySQL Forum
Topic By Replies Updated
MySQL in high availability and transction secure banking application klamor 1 August 28th, 10:24 AM
MySQL rollback UAL225 1 August 28th, 10:15 AM
Browsing a DB file that uses MySql finleytech 1 July 26th, 10:51 AM
php cookie won't pass variable to next page ITdevGirl 0 June 22nd, 12:13 PM