PHP - 无法显示输出文本,如果密码不匹配
问题描述:
一直在挣扎这么久... 故事:我试图在我的注册表中添加“密码确认”($repassword
)。还添加了一个字符串,其中说明“密码不匹配”,如果是这种情况(如果密码不相同),应该激活它。
问题:一切正常,但如果密码不相同,它不会显示该消息。
如果你不赶时间,多一点故事:我尝试了几件事。例如将它放入函数(function valid_pws
)或将它放入单个if语句中。其中一些通过输出“密码不匹配”文本进行工作,但是如果用户点击了“注册”按钮,它总会显示该消息。哦,数据库一切都很好。它总是理解我想要做什么,只是不能显示文本(如果需要)或隐藏(如果不需要)。PHP - 无法显示输出文本,如果密码不匹配
PHP代码:
<?php
require ('insert.php');
function valid_pws($password,$repassword) {
// Validate pws.
if (empty($password || $repassword)) {
return false;
}
else if ($password !== $repassword) {
// error matching passwords
return false;
}
else {
return true; // passwords match
}
}
// create that user
function create_user($name, $username, $password, $email) {
require ('insert.php');
$query = "INSERT INTO `users` (Name, Username, Password, EMail) VALUES('$name','$username', '$password', '$email')";
$result = mysqli_query($connection, $query);
if($result){
return true; // Success
}else{
return false; // Error somewhere
}
}
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
// I'm not using hash atm
$hash = password_hash($password, PASSWORD_DEFAULT);
//valid_pws function in a var
$pwerr = valid_pws($password,$repassword);
//if there's everything fine with passwords then good
if ($pwerr === true){
//if there's everything fine with creating user then good
if (create_user($name, $username, $password, $email)) {
$smsg = "User registration success.";
}
else{
//if there's something wrong with pws, then error. Should get it from function if false is returned... or not?
if($pwerr === false){
$rmsg = "Passwords doesn't match";
}
else {
// if there's something else with registration, then error
$fmsg = "User registration failed.";
}
}
}
}
mysqli_close($connection);
?>
HTML代码:
<form action="" method="POST" class="form-horizontal" role="form">
<!-- How registering went -->
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<?php if(isset($rmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $rmsg; ?> </div><?php } ?>
<!-- Registration form begins -->
<h2>Register</h2><br>
<label for="Name">Name</label>
<input name="name" type="text" id="name" maxlength="40" placeholder="Name" class="form-control" required autofocus> <!-- end -->
<label for="email">Email</label>
<input name="email" type="email" id="email" maxlength="65" placeholder="Email" class="form-control"> <!-- end -->
<label for="userName">User</label>
<input name="username" type="text" id="userName" maxlength="12" placeholder="Username" class="form-control" required> <!-- end -->
<label for="password">Password</label>
<input name="password" type="password" id="password" maxlength="50" placeholder="Password" class="form-control" required> <!-- end -->
<label for="password">Repassword</label>
<input name="repassword" type="password" maxlength="50" placeholder="Password again" class="form-control"> <!-- end -->
<button type="submit" class="btn btn-primary btn-block">Register</button>
</form>
答
你搞乱条件应该是:
if ($pwerr === true){
//if there's everything fine with creating user then good
if (create_user($name, $username, $password, $email)) {
$smsg = "User registration success.";
}
}//<-------- note that coditopn over here
else{
//if there's something wrong with pws, then error. Should get it from function if false is returned... or not?
if($pwerr === false){
$rmsg = "Passwords doesn't match";
}
else {
// if there's something else with registration, then error
$fmsg = "User registration failed.";
}
}
+0
对我感到羞耻......这是愚蠢的(但重要的)错误。不能相信我浪费了3个小时在这个...>。虽然BIG THANKS为你的好眼睛! :) –
答
试图改变这种状况:
if ($pwerr === true){
if (create_user($name, $username, $password, $email)) {
$smsg = "User registration success.";
}else{
$fmsg = "User registration failed.";
}
}else{
$rmsg = "Passwords doesn't match";
}
答
您的代码存在的问题是,它永远不会进入验证密码不匹配的地方。
你可以这样做:
<?php
function valid_pws($password,$repassword) {
// Validate pws.
if (empty($password || $repassword)) {
return false;
} else if ($password !== $repassword) {
// error matching passwords
return false;
}
return true; // passwords match
}
//valid_pws function in a var
$pwerr = valid_pws("teste","tete");
//if there's everything fine with passwords then good
if ($pwerr === true){
//if there's everything fine with creating user then good
if (create_user($name, $username, $password, $email)) {
$smsg = "User registration success.";
} else {
// if there's something else with registration, then error
$fmsg = "User registration failed.";
}
} else {
$rmsg = "Passwords doesn't match";
}
if(isset($smsg)){ echo $smsg; }
if(isset($fmsg)){ echo $fmsg; }
if(isset($rmsg)){ echo $rmsg; }
上面的代码将打印Passwords doesn't match
你的 '如果($ pwerr ===真)' 不具有别的。如果($ pwerr === false){在$ pwerr === true的范围内。这将永远不会是这样的情况 – Johan
哦,我认为情况并非如此..试图解决它。 –