卡住试图防止创建重复用户
Stuck trying to prevent duplicate users being created
我这辈子都找不到这有什么问题。这可能是一些隐藏的规则或愚蠢的错误。此代码旨在防止用户创建相同的用户或相同的电子邮件两次,但仅适用于用户名。我错过了什么还是只是一个愚蠢的错误?我正在使用两个 if 语句来尝试实现此目的(如评论所述),但它不起作用。
提前致谢
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['emailAddress'];
// Create connection
$conn = new mysqli($servername, $SQLusername, $SQLpassword, $DBname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
$sql = "SELECT * FROM `Login` WHERE `Username` LIKE '$username' AND `Password` LIKE '$password';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$emailDB = $row['Email'];
$usernameDB = $row['Username'];
// username validation
if ($username == $usernameDB)
{
echo "Username '$username' is already taken!<br>";
exit;
}
//E-mail validation
if ($email == $emailDB)
{
echo "Email '$email' is already registered!<br>";
exit;
}
您只需要另一个查询:))) :
$sql = "SELECT * FROM `Login`
WHERE `Username` LIKE '$username'
OR `Email` LIKE '$email';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$emailDB = $row['Email'];
$usernameDB = $row['Username'];
// username validation
if ($username == $usernameDB)
{
echo "Username '$username' is already taken!<br>";
exit;
}
//E-mail validation
if ($email == $emailDB)
{
echo "Email '$email' is already registered!<br>";
exit;
}
你最好使用:
"SELECT * FROM `Login`
WHERE `Username` = '$username'
OR `Email` = '$email';"
因为如果不相等,新用户可以注册:-)
我这辈子都找不到这有什么问题。这可能是一些隐藏的规则或愚蠢的错误。此代码旨在防止用户创建相同的用户或相同的电子邮件两次,但仅适用于用户名。我错过了什么还是只是一个愚蠢的错误?我正在使用两个 if 语句来尝试实现此目的(如评论所述),但它不起作用。
提前致谢
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['emailAddress'];
// Create connection
$conn = new mysqli($servername, $SQLusername, $SQLpassword, $DBname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
$sql = "SELECT * FROM `Login` WHERE `Username` LIKE '$username' AND `Password` LIKE '$password';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$emailDB = $row['Email'];
$usernameDB = $row['Username'];
// username validation
if ($username == $usernameDB)
{
echo "Username '$username' is already taken!<br>";
exit;
}
//E-mail validation
if ($email == $emailDB)
{
echo "Email '$email' is already registered!<br>";
exit;
}
您只需要另一个查询:))) :
$sql = "SELECT * FROM `Login`
WHERE `Username` LIKE '$username'
OR `Email` LIKE '$email';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$emailDB = $row['Email'];
$usernameDB = $row['Username'];
// username validation
if ($username == $usernameDB)
{
echo "Username '$username' is already taken!<br>";
exit;
}
//E-mail validation
if ($email == $emailDB)
{
echo "Email '$email' is already registered!<br>";
exit;
}
你最好使用:
"SELECT * FROM `Login`
WHERE `Username` = '$username'
OR `Email` = '$email';"
因为如果不相等,新用户可以注册:-)