所以我正在尝试创建一个登录表单,现在我正处于要验证密码和确认密码的部分。我使用的代码(您可以在下面找到它)每次提交“注册”按钮时都会显示“密码和确认密码不匹配”,即使我输入相同的密码也是如此。知道应该在我的代码中更改什么来消除错误吗?
<?php
session_start();
include('includes/config.php');
include('includes/db.php');
function isUnique($email){
$query="select * from users where email ='$email'";
global $db;
$result = $db->query($query);
if($result->num_rows>0){
return false;
}
else return true;
}
if(isset($_POST['register'])) {
$_SESSION['name']=$_POST ['name'];
$_SESSION['email']=$_POST ['email'];
$_SESSION['password']=$_POST ['password'];
$_SESSION['confirm_password']=$_POST ['confirm_password'];
if(strlen($_POST['name'])<3){
header("Location:register.php?err=".urlencode ("The name must be at least 3 characters long"));
exit();
}
else if(strlen($_POST['password']) < 5 ){
header("Location:register.php?err=".urlencode ("The password should be at least 5 characters"));
exit();
}
else if(strlen($_POST['confirm_password']) < 5 ){
header("Location:register.php?err=".urlencode ("The Confirm password should be at least 5 characters"));
exit();
}
else if(!isUnique($_POST['email'])){
header("location:register.php?err=".urlencode ("Email is already in use. Please use another one"));
exit();
}
else if($_POST['[password'] != $_POST['confirm_password']) {
header("Location:register.php?err=".urlencode ("The password and confirm password do not match"));
exit();
}
}
?>
这是我在应显示错误消息时放置条件的另一部分。
`<form action="register.php" method="post" style="margin-top:35px;">
<h2> Register Here </h2>
<?php
if(isset ($_GET['err'])) { ?>
<div class="alert alert-danger"><?php echo $_GET['err']; ?></div>
<?php }
?>`
最佳答案
你只是有一个小错字:
使用 $_POST['password']
代替 $_POST['[password']
关于php - PHP 中的密码验证和确认密码始终显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274161/