简体   繁体   English

如何检查电子邮件是否已经注册?

[英]How to check if email is already registered?

I am trying to check to see if an email is already registered using PDO, but I have not found a good way to do this so far. 我正在尝试检查是否已使用PDO注册了电子邮件,但是到目前为止,我还没有找到一种很好的方法来进行此操作。 This is what I have so far. 到目前为止,这就是我所拥有的。 And the connection variable is "$con" if that helps. 如果有帮助,则连接变量为“ $ con”。 Also, do I need the connection variable in the same document or will it being in a document that is included work? 另外,我是否需要在同一文档中使用连接变量,还是在包含的文档中使用它?

<link rel="stylesheet" type="text/css" href="css\errors.css">
<?php
require 'header.php';
if (!$_POST['submit']) {
?>
<html>
<a href="register.html">Regsiter here!</a>
<?php
} else {
    require 'connect.php';
    $firstname = ($_POST['firstname']);
    $lastname = ($_POST['lastname']);
    $email = ($_POST['email']);
    $password = ($_POST['password']);
    $passwordconf = ($_POST['passwordconf']);

    $errorfields = "<p class='errormsg'>Please fill out all the fields!</p>";
    if (empty($firstname) || empty($lastname) || empty($email) || empty($password) || empty($passwordconf)) {
        echo "$errorfields";
    }
    $erroremail = "<p class='errormsg'>Email is not in name@domain format!</p>";
    $regex = "/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+.[a-z]{2,}$/i";
    if (!preg_match($regex, $email)) {
        echo "$erroremail";
    }
    $errorpassword = "<p class='errormsg'>You passwords do not match!</p>";
    if ($password != $passwordconf) {
        echo "$errorpassword";
    }

    //this is where email is checked
}
?>

Try this, fill in DB_NAME, DB_USER_NAME, and DB_USER_PASS with your connection credentials or use the $con variable if everything is set up correctly as a PDO object with it: 尝试此操作,使用您的连接凭据填写DB_NAME,DB_USER_NAME和DB_USER_PASS,或者如果所有内容都正确地设置为PDO对象,则使用$ con变量:

$con = new PDO( 'mysql:host=localhost;dbname=DB_NAME;charset=UTF-8', 'DB_USER_NAME', 'DB_USER_PASS' );

$query = $con->prepare( "SELECT `email` FROM `tbl_name` WHERE `email` = ?" );
$query->bindValue( 1, $email );
$query->execute();

if( $query->rowCount() > 0 ) { # If rows are found for query
     echo "Email found!";
}
else {
     echo "Email not found!";
}

There are some issues in the accepted answer (which is quite understandable as it has been written quite a while ago). 接受的答案中有一些问题(这是很容易理解的,因为它已经写了很久了)。

The first one is a bit irrelevant but still. 第一个有点无关紧要,但仍然如此。 The connection code is not as simple as just a single line of code. 连接代码不像单行代码那么简单。 Please refer to my article on how to properly connect with PDO to avoid many possible problems in the future. 请参阅我的文章, 了解如何正确连接PDO以避免将来出现许多可能的问题。

Another issue is the use of rowCount() method, which, as it's stated in the manual, could be unavailable . 另一个问题是使用rowCount()方法,正如手册中所述,该方法可能不可用 Besides, the use of bindParam() is superfluous and could be avoided in order to make the code cleaner and more concise. 此外, bindParam()的使用是多余的,可以避免使用,以使代码更简洁。 As a result, we can come up the following solution 结果,我们可以提出以下解决方案

require 'connect.php'; // contains the code mentioned above.

$stmt = $con->prepare( "SELECT 1 FROM `tbl_name` WHERE `email` = ?");
$stmt->execute([$email]);
$found = $stmt->fetchColumn();

if( $found ) {
    echo "Email found!";
} else {
    echo "Email not found!";
}

In case you don't only need to check the existence but also fetch the actual record (it order to verify the password, for example) simply change 1 in the query to the actual fields and fetchColumn() to fetch() : 如果您不仅需要检查是否存在,还需要获取实际记录(例如,为了验证密码),只需将查询中的1更改为实际字段,然后将fetchColumn()更改为fetch()

$stmt = $con->prepare( "SELECT 1 FROM `tbl_name` WHERE `email` = ?");
$stmt->execute([$email]);
$user= $stmt->fetch();

if( $user ) {
    echo "Email found!";
} else {
    echo "Email not found!";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM