如何检查电子邮件地址是否存在。验证

时间:2011-05-22 10:16:17

标签: php mysql registration

我似乎无法弄清楚如何检查我的数据库中是否存在电子邮件。目前,用户使用他们的电子邮件地址使用密码登录我的网站,但目前用户可以使用相同的电子邮件地址多次注册,这会导致我的网站出现重大问题。做过一些研究,但似乎无法解决。

有人能帮助我吗?

<?php       

if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$db = "db";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not send your enquiry at this time please try again later.');
}


$phone = mysql_real_escape_string((string)$_POST['phone']);
$email = mysql_real_escape_string((string)$_POST['email']);
$password = mysql_real_escape_string((string)$_POST['password']);
$firstname = mysql_real_escape_string((string)$_POST['firstname']);
$surname = mysql_real_escape_string((string)$_POST['surname']);
$country = mysql_real_escape_string((string)$_POST['country']);
$nationality = mysql_real_escape_string((string)$_POST['nationality']);
$yearofbirth = mysql_real_escape_string((string)$_POST['yearofbirth']);  
$profession = mysql_real_escape_string((string)$_POST['profession']); 
$status = mysql_real_escape_string((string)$_POST['status']); 
$membertype = 'Registered';
$dateregistered = mysql_real_escape_string((string)$_POST['dateregistered']); 
$agreedtoterms = mysql_real_escape_string((string)$_POST['agreedtoterms']); 

$sql = "INSERT INTO members
       (phone, email, password, firstname, surname, country, nationality, yearofbirth, profession, uniquepin, status, membertype, dateregistered, agreedtoterms)
       VALUES('$phone', '$email', '$password', '$firstname','$surname','$country','$nationality','$yearofbirth','$profession','$uniquepin','$status','$membertype','$dateregistered', '$agreedtoterms')";


mysql_select_db($db);
$retval = mysql_query( $sql, $conn )or die(mysql_error());

?>

3 个答案:

答案 0 :(得分:5)

您可以使email列具有唯一约束 - 然后在尝试插入时查询将失败。

您也可以查询它....

SELECT `email`
  FROM `members`
 WHERE `email` = '$email'
 LIMIT 1

如果您收到结果,则电子邮件存在。

答案 1 :(得分:1)

在登录表上添加唯一约束之前,他需要将重复的电子邮件记录(否则约束将失败)合并到一个记录中并通知受影响的用户。

如果电子邮件在数据级别是唯一的,则可以添加约束。最后,在创建任何新的登录记录之前,他运行查询以查看是否存在电子邮件添加。

应该是一个SO线程:基于电子邮件的登录101; - )

答案 2 :(得分:-2)

$query_ch_email = mysql_query("SELECT email FROM members WHERE email = '$email' LIMIT 1") or die(mysql_error());

    if (mysql_num_rows($query_ch_email) > 0)
    {
    $notices['register'][] = 'Your e-mail already exist'; 
    }

    if (!count($notices['register']))
    {
    $sql_insert = "INSERT INTO members (

      phone
    , email
    , password
    , firstname
    , surname
    , country
    , nationality
    , yearofbirth
    , profession
    , uniquepin
    , status
    , membertype
    , dateregistered
    , agreedtoterms

    ) VALUES (

      '$phone'
    , '$email'
    , '$password'
    , '$firstname'
    , '$surname'
    , '$country'
    , '$nationality'
    , '$yearofbirth'
    , '$profession'
    , '$uniquepin'
    , '$status'
    , '$membertype'
    , '$dateregistered'
    , '$agreedtoterms'

    )";

    mysql_query($sql_insert)or die(mysql_error());
    }