我试图强制用户选择一个唯一的用户名并在他们不知道的情况下返回错误消息。所有其他验证(密码匹配等)都在工作,但验证未使用的用户名只会返回ID注册失败消息。
我已根据要求使用HTML更新了此内容。
我只是想让它告诉用户我在这些情况下所概述的错误。
<?php
require('connect.php');
if(isset($_POST) & !empty($_POST)){
// If the values are posted, insert them into the database.
// if (isset($_POST['app_id']) && isset($_POST['password'])){
$app_id = mysqli_real_escape_string($connection, $_POST['app_id']);
$first = mysqli_real_escape_string($connection, $_POST['first']);
$last = mysqli_real_escape_string($connection, $_POST['last']);
$gender = mysqli_real_escape_string($connection, $_POST['gender']);
$birth = mysqli_real_escape_string($connection, $_POST['birth']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, md5($_POST['password']));
$confirmpassword = mysqli_real_escape_string($connection, md5($_POST['confirmpassword']));
if($password == $confirmpassword){
$fmsg = "";
//username validation
$newidvalq = "SELECT * FROM 'user' WHERE app_id='$app_id'";
$newidres = mysqli_query($connection, $newidvalq);
$idcount = 0;
$idcount = mysqli_num_rows($newidres);
if($idcount >= 1){
$fmsg .= "That app ID is already being used, please try a different ID";
}
//email validation
$emailvalq = "SELECT * FROM 'user' WHERE email='$email'";
$emailres = mysqli_query($connection, $emailvalq);
$emailcount = 0;
$emailcount = mysqli_num_rows($emailres);
if($emailcount >= 1){
$fmsg .= "That email is already being used";
}
//DB Insert
$query = "INSERT INTO `user` (app_id, first, last, gender, birth, password, email) VALUES ('$app_id', '$first', '$last', '$gender', '$birth', '$password', '$email')";
//Result Validation
$result = mysqli_query($connection, $query);
if($result){
$smsg = "app ID Created Successfully";
}else{
$fmsg .= "app Registration Failed";
}
}else{
$fmsg = "Your Passwords do not match";
}
}
?>
<html>
<head>
<title>User Registeration Using PHP & MySQL</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="styles.css" >
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form class="form-signin" method="POST">
<?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 } ?>
<h2 class="form-signin-heading">Please Register</h2>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" name="app_id" class="form-control" placeholder="app ID" value="<?php if(isset($app_id) & !empty($app_id)) {echo $app_id;} ?>" required>
</div>
<input type="text" name="first" class="form-control" placeholder="First Name" value="<?php if(isset($first) & !empty($first)) {echo $first;} ?>"required>
<input type="text" name="last" class="form-control" placeholder="Last Name" value="<?php if(isset($last) & !empty($last)) {echo $last;} ?>"required>
<input type="text" name="gender" class="form-control" placeholder="Gender" value="<?php if(isset($gender) & !empty($gender)) {echo $gender;} ?>"required>
<input type="date" name="birth" class="form-control" placeholder="Birthday" required>
<label for="inputEmail" class="sr-only">Email Address</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" value="<?php if(isset($email) & !empty($email)) {echo $email;} ?>"required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Enter a Password" required>
<label for="inputPassword" class="sr-only">RetypePassword</label>
<input type="password" name="confirmpassword" id="inputPassword" class="form-control" placeholder="Confirm Your Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
<a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
在使用之前首先为$idcount
分配0,然后在if条件中使用($idcoun>=1)
根据你的逻辑,如果用户ID和电子邮件也存在,那么你也根据需要插入没有意义的数据
答案 1 :(得分:0)
将您的INSERT块包含在:
中if($ fmsg ==“”){.. to here ..}
因此,如果出现错误,则不会将任何记录插入数据库,并显示错误。
将动作属性添加到表单标记中,如:
<form class="form-signin" method="POST" action="">
某些版本的html中不需要html5。