在 PHP 中验证唯一的用户 ID,如果不是唯一的则返回错误 [重复]
Posted
技术标签:
【中文标题】在 PHP 中验证唯一的用户 ID,如果不是唯一的则返回错误 [重复]【英文标题】:Validating a unique userid in PHP, returning error if not unique [duplicate] 【发布时间】:2018-03-24 07:14:23 【问题描述】:我试图强制用户选择一个唯一的用户名,如果他们不这样做,则会返回一条错误消息。所有其他验证(密码匹配等)都在工作,但验证未使用的用户名只会返回 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>
【问题讨论】:
db表字段user_id是整数?如果您将它与字符串用户名进行比较,那么它总是会失败。 您可能需要一个以正确格式存储用户名的新表列。 我是否在某处将 user_id 声明为整数? user表中的user_id是什么数据类型? 你也可以发布你的html代码吗? 【参考方案1】:在使用之前先将$idcount
赋值为0,然后在if条件中使用($idcoun>=1)
并且根据您的逻辑,如果用户 ID 和电子邮件也存在,那么您也将根据您的需要插入没有意义的数据
【讨论】:
我按照您的建议进行了更改,但仍然没有骰子。超级奇怪。 检查您的查询和数据库 当用户 ID 和电子邮件不存在于数据库中时,您能否发送输出并使用 action = " "【参考方案2】:将您的 INSERT 块包含在:
if( $fmsg == "" ) ..到这里..
这样如果有错误,就不会向数据库插入记录,并且会显示错误。
将 action 属性添加到表单标签中,例如:
<form class="form-signin" method="POST" action="">
在某些版本的 html 而非 html5 中是必需的。
【讨论】:
以上是关于在 PHP 中验证唯一的用户 ID,如果不是唯一的则返回错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 4 表单验证在更新表单上应该是唯一的,但如果是当前的则不是