一直在为此苦苦挣扎......故事:我试图在$repassword
我的注册表中添加“密码确认”()。还添加了一个字符串,上面写着“密码不匹配”,如果是这种情况应该激活(如果密码不同)。
问题:一切正常,但如果密码不同,它不会显示该消息。
如果你不着急,再讲一个故事:我已经尝试了几件事。例如,将其放入函数 ( function valid_pws
) 或将其放入单个 if 语句中。其中一些通过输出“密码不匹配”来工作” 文本,但是如果用户单击“注册”按钮,它总是显示该消息。哦,数据库一切正常。它总是理解我想要做什么,只是不能显示该文本(如果需要)或隐藏(如果不需要)。
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>