2

我有一个简单的报名表,其中包括一个电子邮件地址。我想检查重复项(我可以这样做),但我正在努力的是如何让输入表表明它是重复的“请再试一次”。

报名表代码如下:

  <form action="mailer2.php" method="POST">
    <div>
        <p class="auto-style1" style="width: 408px">Newsletter Sign-Up Form</p>
        <p>First Name</p>
        <input name="firstname" type="text"> <br> </div>
        <div>
        <p>Last Name</p>
        <input name="lastname" type="text">
        <br>
        </div>
        <p>E-Mail</p>
        <input name="email" type="text">
        <br>
        </div>
        <div>
        <p>What are your interests"</p><br>
        <input type="checkbox" name="activity[]" value="run">I enjoy running<br>
        <input type="checkbox" name="activity[]" value="bike">
        I enjoy mountain biking<br>

        <input type="checkbox" name="activity[]" value="hike">I enjoy hiking<br>
        </div>
        <div>
    <input name="submit" type="submit" value="Send!"> </div>
</form>

PHP 代码:这是我卡住的地方......我如何返回我的表单并在电子邮件地址下方注明它是重复的

    <?php
$hostname = "hostname";
$username = "Username";
$password = "password";
$dbname = "Tablename";
$TableName = "MemberInfo";

// Check connections
mysql_connect($hostname, $username, $password) or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
// Get values from form
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$email=$_POST['email'];
$FullName = $fname . " " . $lname;
$activity=$_POST['activity'];
$run = 0;
$bike = 0;
$hike = 0;

//Check for Duplicate Email
$result = mysql_query("SELECT * FROM MemberInfo WHERE Email='$email'");
$DupCheck = mysql_num_rows($result);

if ($DupCheck) {
echo "Email already exists ... please try again.";
//header('Location: NewsletterSignUp.html');
exit;

//trigger_error('Email Already Exists.', E_USER_WARNING);
}

if (isset($_POST['activity'])) {
    $activity = $_POST['activity'];
    foreach($activity as $key => $value) {
        //echo $key. ' = ' . $value . '<br>';
        if ($value == 'run') {
        $run = 1;
        }
        if ($value == 'bike') {
        $bike = 1;
        }
        if ($value == 'hike') {
        $hike = 1;
        }
        }
        } else {
            echo 'Nothing';
        }

// Insert data into  mysql
$sql="INSERT INTO $TableName (FirstName, LastName, FullName, 
    Email, Run, Bike,     Hike) VALUES('$fname', '$lname', 
    '$FullName', '$email', '$run', 
    '$bike', '$hike')";
$result=mysql_query($sql);
 // if successful insert data into database, displays message "Successful".
if($result){
header('Location: Confirmation.html');
}
else {
die('Invalid query: ' . mysql_error());
}
if(isset($_POST['submit'])) {

// Send email 
$to = "someone@example.com";
$subject = "Newsletter Sign-Up";

// data the visitor provided
$fname_field = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$lname_field = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
foreach($_POST['activity'] as $value) {
$activity_field .= "Checked: $value\n";
}
//constructing the message
$body = " From: $fname_field $lname_field\n\n 
    E-Mail: $email_field\n\n$activity_field";

// ...and away we go!
mail($to, $subject, $body);

} else {

// handle the error somehow

}

?>
<?php
// close connection
mysql_close();
?>

(编辑 - 显示表单 html)

4

1 回答 1

2

最简单的方法是将电子邮件字段设置为具有索引的 UNIQUE,然后将无法添加 2 个相同的电子邮件字段。另一种方法是使用该邮件进行选择,并检查该字段是否有记录。

也不要使用 mysql_* 函数,它们是旧的,将来会被删除。

这里有更多关于唯一索引http://dev.mysql.com/doc/refman/5.0/en/create-index.html

以我个人的经验,我喜欢将很多东西放入数据库,如果有可以通过数据库检查的东西,为什么不使用它呢?

更改您的数据库

ALTER TABLE $TABLE
ADD UNIQUE INDEX Email(Email); 

或者

CREATE UNIQUE INDEX unique_email
ON $TABLE (Email)

在已经有电子邮件的地方添加新行将返回错误,或者如果您使用 pdo 它将引发异常。就像是:

ERROR 1062 (23000): Duplicate entry 'xxx' for key 1.
于 2013-04-16T13:51:27.693 回答