1 回答
TA贡献1850条经验 获得超11个赞
您必须检查该电子邮件是否存在于您的用户表中。像这样的东西。
<?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");
}
}
}
?>
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报