use CGI;
use strict;
use DBI();
use CGI::Carp qw(fatalsToBrowser);
print "content-type: text/html; charset=iso-8859-1\n\n";
my $q = new CGI;
my $name = $q->param('name');
my $email = $q->param('email');
print $q->header;
#connect to database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","root",
"mukesh", {'RaiseError' => 1});
eval { $dbh->do("CREATE TABLE IF NOT EXISTS emp (name VARCHAR(20), email VARCHAR(50) UNIQUE NOT NULL)") };
print "creating table emp failed: $@" if $@;
print "<br>";
$dbh->do("INSERT INTO emp(name,email) values('$name','$email')");
my $sql = qq/select * from emp order by name/;
my $sth = $dbh->prepare($sql) or die "Can't prepare $sql:$dbh->errstrn";
my $rv = $sth->execute() or die "can't execute the query: $sth->errstrn";
while (my @row = $sth->fetchrow_array) {
print join(", ",@row),"<br>";
}
$sth->finish();
$dbh->disconnect();
print "<br>Total no of records : $rv";
if ($rv>=1){
print "<br>Record has been successfully updated !!!<br>";
} else {
print "<br>Error!!while inserting record<br>";
exit;
}
当我为相同的电子邮件地址值提交 html 表单时,我被重定向到另一个页面,并出现以下错误:
Content-Type: text/html; charset=ISO-8859-1
Software error:
DBD::mysql::db do failed: Duplicate entry 'rajesh@gmail.com' for key 'email' at C:/Apache2/cgi-bin/connectivity.cgi line 27.
1)我不希望显示此错误,如果电子邮件地址已经存在,我想显示我自己的消息。类似“电子邮件 ID 已存在,请输入新 ID”
2)解决上述问题后,我希望它显示在我提交表单的同一页面上。
此行似乎因重复输入而失败
$dbh->do("INSERT INTO emp(name,email) values('$name','$email')");