PHP - 检查重复条目

时间:2013-04-16 13:43:02

标签: php

我有一个简单的报名表,其中包含一个电子邮件地址。我想检查一个副本(我可以做),但我正在努力的是如何获得报名表格,表明它是重复的“请再试一次”。

报名表格代码如下:

  <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)

1 个答案:

答案 0 :(得分:2)

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

也不要使用mysql_ *函数,它们已经过时了,将来会被删除。

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

的信息

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

改变你的Db

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.