我正在尝试检查是否已使用PDO注册了电子邮件,但是到目前为止,我还没有找到一种很好的方法来进行此操作。到目前为止,这就是我所拥有的。如果有帮助,则连接变量为“ $ con”。另外,我是否需要在同一文档中使用连接变量,还是在包含的文档中使用它?
<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 } ?>
尝试此操作,使用您的连接凭据填写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!"; }