I am relatively new to this list and perl and DBI as well.  The goal is that
before users submit a form submission to the database, I want to make sure
that someone with the same email address has not already submitted it.  The
Email field is marked as UNIQUE() in the MySQL database table, so when
someone trys to enter twice, there is an error and the record does not get
entered.  However, I would like to display a custom page that nicely
explains that we apprecitate their interest, but they have already been
entered into the database.  With this code, I recieve the Carp error in the
browser.  When I turn off Carp, the script acts as if the entry was added
(no error displayed), but the record is not entered.  Can anyone offer
suggestions on how to make decisions based on a DBI error message ?

The code that adds people to a MySQL database table using the following
insert statement:

#! /usr/bin/perl -T -w
# survey.pl

use strict;
use lib qw(/home/ahughes/myLibrary);
use CGI qw(:standard escape escapeHTML);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use WebDB;

....

sub insert_data {

my $dbh = WebDB::connectSurvey ();
my $sth;

$dbh->do (qq{INSERT INTO nationalConsumerShoppingSurvey SET 
participantId = ?,
participantDate = NOW(),
participantFirstName = ?,
participantLastName = ?,
participantPhone = ?,
participantPhoneExtension = ?,
participantDivision = ?,
participantEmail = ?
},

undef,
undef,
$participantFirstName,
$participantLastName,
$participantPhone,
$participantPhoneExtension,
$participantDivision,
$participantEmail
) or surveyErrTrap("Cannot enter record because of duplicate email");
return my $dbh;

$dbh->disconnect ();

}

sub surveyErrTrap 
{
   my $error_message = shift(@_);
   die "$error_message\n  ERROR: $DBI::err ($DBI::errstr)\n";
}#end: err_trap

.....

######################################


Thanks,
Andrew

Reply via email to