0
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')");
4

2 回答 2

4

正如其他人所说,您可以测试电子邮件是否已经存在,但是如果有人提交您的表单并且在您的代码的另一个副本已经测试电子邮件不存在之后它进入插入,会发生什么情况。最好通过将插入包装在 eval 中(因为您已经启用了 RaiseError )并随后测试 err 和 state 以查看错误是否是重复条目来简单地捕获这种情况。

eval {
  $dbh->do("INSERT INTO emp(name,email) values(?,?)", undef, $name, $email);
};
if ($@) {
  # you might want to use state instead of err but you did not show us the state
  if ($dbh->err =~ /Duplicate entry/) {
    # already registered
  } else {
    # report what is in $@ - it is a different error
  }
}

顺便说一句,由于您启用了 RaiseError,您不需要继续检查各种 DBI 方法的返回(您的“或死亡”) - 如果它们失败,它们将自动死亡,并且 PrintError 默认为 1,因为您发现它们会打印错误。

最后一件事,胖逗号 (=>) 自动将事物串到左边,因此“{'RaiseError' => 1}”可以写成“{RaiseError => 1}”。

于 2013-09-17T08:13:43.080 回答
3

要处理此问题,您可以检查电子邮件地址是否已存在。

my $sth = $dbh->prepare("SELECT COUNT(*) FROM emp WHERE email = ?");
$sth->execute($email);
my $rows = $sth->fetchrow_arrayref();
$sth->finish();

if ($rows->[0] == 0) {
    # Insert your email address
} else {
    # Show an error message
}

正如 amon 指出的那样,您应该始终在语句中使用占位符。

于 2013-09-16T23:45:38.413 回答