我使用 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");
}
}
}
?>
慕盖茨4494581