#!/usr/bin/perl -w
use strict;
use diagnostics;
use CGI;     # see https://www.perl.com/CPAN
use Mysql;

# Create an instance of CGI
my $query = new CGI;

# Output a basic MIME type
print $query->header("text/html");

# Grab information the user gave us, and verify it
my $firstname = $query->param("firstname");
my $lastname = $query->param("lastname");"

# Make sure that the user has entered a plausible
# address
my $email = $query->param("email");
&log_and_die(
"You entered an invalid e-mail address!")
 unless ($email =~ /@.*\..*/);

# Gather birthday information
my $birthmonth = $query->param("month");
my $birthday = $query->param("day");
my $birthyear =
 1900 + (10 * $query->param("yeartens")) +
$query->param("yearones");

# Create a date string from that information
my $birthdate =
"$birthyear-$birthmonth-$birthday";"

# Now that we have the basic information, create
# an SQL query
my $command = "insert into birthdays ";
$command .=
"(firstname, lastname, email, birthdate) ";
$command .= "values ";
$command .= "(\"$firstname\",
\"$lastname\", \"$email\",
\"$birthdate\")";

# Connect to the database server
my $dbh = Mysql->connect("localhost",
"test");

# Send the query for processing
my $sth = $dbh->query($command);

# Check for duplication errors, then for other
# errors
if ($dbh->errno == 2000)
 {
  &log_and_die(
"There is already an entry in the database for \"
$email\". Try another e-mail address!");
 }
elseif (!defined $sth)
 {
  &log_and_die("MySQL error " . $dbh->errno .
" on command \"$command\"<P>"
. $dbh->errmsg) unless (defined $sth);
 }

# If there were no errors, then send results to
# the user
else
 {
  # Return something to the user
  print "<P>Done!</P>\n";
 }