如何查看邮箱是否已被使用

我使用 PHP 进行了表单验证。如果发生错误,错误消息将显示在每个输入列周围。我想检查一下这个邮箱是否被使用过。因此,我使用错误代码来定义是否使用输入电子邮件地址,然后显示“电子邮件已被使用”的错误消息。然而,结果变成了我输入的任何内容,它只显示“电子邮件已被使用”。有人可以帮我解决这个问题吗?谢谢!


<?php

  require_once('./conn.php');

  $errorMsgs = array('nickname'=>'', 'email'=>'', 'password'=>'');

  

  if(isset($_POST['submit'])) {

    if(empty($_POST['nickname'])) {

      $errorMsgs['nickname'] = "Please enter your nickname";

    }


    $email = $_POST['email'];

    $password = $_POST['password'];

    

    // checking the email is valid or empty

    if(empty($_POST['email'])) {

      $errorMsgs['email'] = "Please enter your email";

    } else {

      if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {

        $errorMsgs['email'] = "Please enter a valid email";

      }

    }


    $errorCode = $conn->errno;

    if($errorCode === 1062) {

      $errorMsgs['email'] = "The email has been used";

    }


    // checking the password is valid or empty

    if(empty($_POST['password'])) {

      $errorMsgs['password'] = "Please enter your password";

    } else {

      if(!preg_match('/\w{8,}/', $password)) {

        $errorMsgs['password'] = "Please enter at least 8 characters";

      }

    }

    

    if(!array_filter($errorMsgs)) {

    $sql = sprintf("INSERT INTO member (nickname, email, password) values ('%s', '%s', '%s')", $_POST['nickname'], $_POST['email'],$_POST['password']);


    $result = $conn->query($sql);

    if($result) {

      header("Location: index.php");

    }

  }

}

?>


侃侃无极
浏览 101回答 1
1回答

慕盖茨4494581

您必须检查该电子邮件是否存在于您的用户表中。像这样的东西。<?php&nbsp; require_once('./conn.php');&nbsp; $errorMsgs = array('nickname'=>'', 'email'=>'', 'password'=>'');&nbsp;&nbsp;&nbsp; if(isset($_POST['submit'])) {&nbsp; &nbsp; if(empty($_POST['nickname'])) {&nbsp; &nbsp; &nbsp; $errorMsgs['nickname'] = "Please enter your nickname";&nbsp; &nbsp; }&nbsp; &nbsp; $email = $_POST['email'];&nbsp; &nbsp; $password = $_POST['password'];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // checking the email is valid or empty&nbsp; &nbsp; if(empty($_POST['email'])) {&nbsp; &nbsp; &nbsp; $errorMsgs['email'] = "Please enter your email";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {&nbsp; &nbsp; &nbsp; &nbsp; $errorMsgs['email'] = "Please enter a valid email";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //you should use sql parameter binding&nbsp; &nbsp; &nbsp;$email = $_POST['email'];&nbsp; &nbsp; $checkDuplicate= $conn->query("SELECT email FROM user_table where email = '$email'");&nbsp; &nbsp; if(!empty($checkDuplicate)) {&nbsp; &nbsp; &nbsp; $errorMsgs['email'] = "The email has been used";&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; // checking the password is valid or empty&nbsp; &nbsp; if(empty($_POST['password'])) {&nbsp; &nbsp; &nbsp; $errorMsgs['password'] = "Please enter your password";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; if(!preg_match('/\w{8,}/', $password)) {&nbsp; &nbsp; &nbsp; &nbsp; $errorMsgs['password'] = "Please enter at least 8 characters";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(empty($errorMsgs)) { //you need to check if there's any error&nbsp; &nbsp; $sql = sprintf("INSERT INTO member (nickname, email, password) values ('%s', '%s', '%s')", $_POST['nickname'], $_POST['email'],$_POST['password']);&nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; if($result) {&nbsp; &nbsp; &nbsp; header("Location: index.php");&nbsp; &nbsp; }&nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP