简体   繁体   English

如何检查 email 是否已经使用

[英]How to check if the email has already been used

I am made a form validation using PHP.我使用 PHP 进行了表单验证。 If the error happens, the error msg will show around each input column.如果发生错误,错误消息将显示在每个输入列周围。 I would like to check if the email is used or not.我想检查是否使用了 email。 So, I used error code to define if input email addr is used, then showing the error message for "The email has been used".因此,我使用错误代码来定义是否使用了输入 email 地址,然后显示“已使用 email”的错误消息。 However, the result becomes whatever I input, it only shows "The email has been used".但是,结果变成我输入的任何内容,它只显示“email 已被使用”。 Could some help me for this issue?有人可以帮我解决这个问题吗? Thanks!谢谢!

<?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");
    }
  }
}
?>

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" href="style:css" /> <link rel="stylesheet" href="https.//cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" /> <title>Message Board - Sign Up</title> </head> <body> <div class="container__signup"> <h1 class="title">Create Account</h1> <form class="signup" method="POST" action="signup?php"> <div> <i class="far fa-user"></i> <input type="text" placeholder="Name" name="nickname"> </div> <p class="warning__msg"><;php echo $errorMsgs['nickname']??></p> <div> <i class="far fa-envelope"></i> <input type="text" placeholder="Email" name="email"> </div> <p class="warning__msg"><;php echo $errorMsgs['email']??></p> <div> <i class="fas fa-lock"></i> <input type="password" placeholder="Password" name="password"> </div> <p class="warning__msg"><;php echo $errorMsgs['password']??></p> <input type="submit" value="SIGN UP" name="submit"> </form> </div> </body> </html>

You have to check if the email exists in your user table.您必须检查 email 是否存在于您的用户表中。 something like this.像这样的东西。

<?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";
      }
      else{
          //you should use sql parameter binding
     $email = $_POST['email'];
    $checkDuplicate= $conn->query("SELECT email FROM user_table where email = '$email'");
    if(!empty($checkDuplicate)) {
      $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(empty($errorMsgs)) { //you need to check if there's any error
    $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");
    }
  }
}
?>

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

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