#!/usr/bin/perl ####################### Imported Modules ####################### use CGI qw/:standard/; # http://search.cpan.org/~lds/CGI.pm-3.05/CGI.pm use DBI; # http://search.cpan.org/~timb/DBI/DBI.pm ##################### Helper Functions ######################### sub MySQLize($); # Turn a string into something insert-able by MySQL ##################### Function Definitions ##################### # These are the functions you modify to do whatever you want. sub Init(); # Connects to database, sets up global variables # Should return an integer >=0 on success, <0 on failure sub Quit(); # Disconnects from database. No return value. sub Perform($); # Attempts to perform a database operation. # Should return an integer >=0 on success, <0 on failure sub PrintError($); # Prints CGI output on failure. No return value. sub PrintSuccess($); # Prints CGI output on success. No return value. ####################### Global Variables ####################### # Username for database, database name, table name, and optional password my $user="dbuser",$dbase="dbname",$table="tablename",$pass=""; my $sth,$dbh,@row; ########################## Entry Code ########################## $errval=Init(); # Attempt to connect, etc. if($errval<0) # On failure, print error and exit { PrintError($errval); exit(1); } $errval=Perform($errval); # Attempt to perform database operation Quit(); # Disconnect from database if($errval < 0 ) # If it returned error, print error then exit { PrintError($errval); exit(1); } else # Otherwise, print success and exit { PrintSuccess($errval); exit(0); } #################### Function Implementations ################## sub Init() { if($pass eq "") { if(!($dbh = DBI->connect("DBI:mysql:$dbase",$user))) { print STDERR "Couldn't connect to database: ",DBI->errstr,"\n"; return(-1); } } elsif(!($dbh = DBI->connect("DBI:mysql:$dbase",$user,$pass))) { print STDERR "Couldn't connect to database with pass: ",DBI->errstr,"\n"; return(-1); } else { return(0); } } sub Perform($) { my $val=shift; my $query="INSERT INTO $table(atime,content)VALUES(NOW(),". MySQLize("QWERTYUIOP").");"; if(!($sth=$dbh->prepare($query))) { return(1); } elsif(!($sth->execute())) { return(1); } else { return(0); } } sub PrintSuccess($) { my $val=shift; print header; print < Success

Success

The database operation completed successfully. EOF ; } sub PrintError($) { my $val=shift; print header; print < Database Error

Database Error

There has been an error communicating with the database, the operation could not be completed. Please contact the administrator. EOF ; } sub Quit() { $dbh->disconnect(); } sub MySQLize($) { my $str=shift; $str =~ s/"/\\"/g; return("\"$str\""); }