[英]How to separately check if PHP username or email exists in database
Right now my code checks if both the username and email variables exist in the database because they are both unique keys, but how do I make it so I can check each one individually and output different responses?现在我的代码检查用户名和电子邮件变量是否都存在于数据库中,因为它们都是唯一的键,但是我该如何制作它以便我可以单独检查每个并输出不同的响应?
Such as if the username exists -> "That username already exists."例如如果用户名存在->“那个用户名已经存在。” Or if the email exists, but username does not -> "That email already exists."
或者,如果电子邮件存在,但用户名不存在 ->“该电子邮件已经存在。”
<?php
include 'include/connection.php';
$username = mysqli_real_escape_string($conn, $_REQUEST['username']);
$password = mysqli_real_escape_string($conn, $_REQUEST['password']);
$name = mysqli_real_escape_string($conn, $_REQUEST['name']);
$email = mysqli_real_escape_string($conn, $_REQUEST['email']);
$sql = "INSERT INTO userdata (username, password, name, email) VALUES ('$username', '$password', '$name', '$email')";
if (mysqli_query($conn, $sql)) {
echo "Your account was created successfully, please login to continue.";
echo '<form action="login.php">
<input type="submit" name="login" id="login" value="Login" />
</form>';
} else {
echo "That username already exists, please try again.";
echo '<form action="registration.php">
<input type="submit" name="register" id="register" value="Register" />
</form>';
}
?>
One of the possible way of having a function and write SQL to check for email
and username
both.使用函数并编写 SQL 来检查
email
和username
的可能方法之一。
SELECT username, email from userdata where username = ? OR email = ?
And, loop through the resultset and check if email
or username
exists and return error message accordingly.并且,遍历结果集并检查
email
或username
存在并相应地返回错误消息。
Try This尝试这个
$checkUserNameSql = "Select Count(username) as username from userdata WHERE username = '".$username."'";
$result=mysql_query($checkUserNameSql);
$data=mysql_fetch_assoc($result);
echo $data['username'];
If you get a count you can apply your check on that.如果你得到一个计数,你可以申请你的支票。
Used below code.使用下面的代码。 first you need to check username and email then insert the record.
首先您需要检查用户名和电子邮件,然后插入记录。
include 'include/connection.php';
$username = mysqli_real_escape_string($conn, $_REQUEST['username']);
$password = mysqli_real_escape_string($conn, $_REQUEST['password']);
$name = mysqli_real_escape_string($conn, $_REQUEST['name']);
$email = mysqli_real_escape_string($conn, $_REQUEST['email']);
$checkUser = 'select email ,username from userdata where email="'.$email.'" or username = "'.$username.'"';
$result = mysqli_query($conn,$checkUser);
$row = mysqli_fetch_assoc($result);
if(!empty($row)){
if($row['email']==$email){
echo "That email already exists. Please try again";
}
else if($row['username']==$username){
echo "That username already exists. Please try again";
}
echo '<form action="registration.php">
<input type="submit" name="register" id="register" value="Register" />
</form>';
}
else{
$sql = "INSERT INTO userdata (username, password, name, email) VALUES ('$username', '$password', '$name', '$email')";
if (mysqli_query($conn, $sql)) {
echo "Your account was created successfully, please login to continue.";
echo '<form action="login.php">
<input type="submit" name="login" id="login" value="Login" />
</form>';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.