gpt4 book ai didi

php - 检查 MySQL 数据库中是否存在没有唯一键的电子邮件地址

转载 作者:行者123 更新时间:2023-11-29 11:41:33 26 4
gpt4 key购买 nike

所以我对应该如何做到这一点有点困惑,一开始我尝试将用户名检查和电子邮件检查合而为一,看看这是否可行,它会针对用户名进行检查,但不会针对电子邮件。

所以我尝试将它们分开。但在这里我感到困惑,因为我不确定是否可以执行两次相同的查询并期望它能够正常工作而不会出现错误。

我对此还很陌生,如果这个问题被认为是“愚蠢的”,我很抱歉。

提交后,我被重定向到我的注册页面,该页面点击代码中倒数第二个 if/else 语句。 “您的密码不匹配...”

我可以对我的用户名和电子邮件使用相同类型的查询两次吗?或者还有其他我应该做的事情吗?我只是不确定是什么。

这是我的 register.php 页面的代码:

<?php

ini_set('display_errors', 1);
ini_set('html_errors', 0);
error_reporting(E_ALL);

require 'connection.php';

$message = '';

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];

if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) > 6 && strlen($password) < 32):

if($password == $confirmpassword):
$records = $conn->prepare('SELECT * FROM users WHERE Username = :username');
$records->bindParam(':username', $username);
$records->execute();
$results = $records->fetchAll(PDO::FETCH_ASSOC);

//var_dump($results);

if(count($results) > 0):
echo 'That username is already in use, please use a different one.';

$emailRecords = $conn->prepare('SELECT * FROM users WHERE Email = :email');
$emailRecords->bindParam(':email', $_POST['email']);
$emailRecords->execute();
$emailResults = $emailRecords->fetchAll(PDO::FETCH_ASSOC);

if(count($emailResults) > 0):
echo 'That email is already in use, are you sure you\'re not registered with us already? <a href="loginPage.php">Login Here</a>';

else:

$sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
$stmt = $conn->prepare($sql);
$hashPassword = password_hash($password, PASSWORD_DEFAULT);

$stmt->bindParam(':firstname', strip_tags($_POST['firstname']));
$stmt->bindParam(':lastname', strip_tags($_POST['lastname']));
$stmt->bindParam(':role', $_POST['role']);
$stmt->bindParam(':email', strip_tags($_POST['email']));
$stmt->bindParam(':username',strip_tags($_POST['username']));
$stmt->bindParam(':password',strip_tags($hashPassword));

if ($stmt->execute()):
echo 'Well done! You have successfully registered with us!';
header('Location:loginPage.php');
else:
echo 'There seems to be an issue getting you registered.';
//$message = 'There seems to be an issue getting you registered.';
endif;
endif;

else:
echo 'Your passwords do not match, please enter them correctly.';
//$message = 'Your passwords do not match, please enter them correctly.';
header('Location:registerPage.php');
endif;

else:
echo 'Your password must be between 6 to 32 characters.';
//header('Location:registerPage.php');
endif;

endif;

endif;
?>

最佳答案

出于对一切神圣事物的热爱,请停止使用 if: else: endif;这就是搞乱你的程序的原因。

你的个人作品看起来不错,但你的 if 语句很困惑。当使用大括号进行漂亮的样式和格式设置时,您将执行以下操作:

if (!empty($_POST['username']) && !empty($_POST['password']))
{
if(strlen($password) > 6 && strlen($password) < 32)
{
if($password == $confirmpassword)
{
if(count(getUsersWithThisUsername($_POST['username'])) > 0)
{
echo 'That username is already in use, please use a different one.';
if(count(getUsersWithThisEmail($_POST['email'])) > 0)
{
echo 'That email is already in use, are you sure you\'re not registered with us already? <a href="loginPage.php">Login Here</a>';
}
else
{
if (tryToCreateaUser())
{
echo 'Well done! You have successfully registered with us!';
}
else
{
echo 'There seems to be an issue getting you registered.';
}
}
}
else
{
echo 'Your passwords do not match, please enter them correctly.';
}
}
else
{
echo 'Your password must be between 6 to 32 characters.';
}
}
}

您可能会发现这没有多大意义。

此外,如果您查看此代码,您会注意到我如何将其中的几个部分放入单独的函数中(未显示)。这是另一个可以用来帮助您发现错误的技巧。通过将您知道正确的小而完整的工作单元提取到单独的函数中,其余部分将变得更易于阅读。

结构应该是这样的:

if (!empty($_POST['username']) && !empty($_POST['password']))
{
if(strlen($password) > 6 && strlen($password) < 32)
{
if($password == $confirmpassword)
{
if(count(getUsersWithThisUsername($_POST['username'])) > 0)
{
echo 'That username is already in use, please use a different one.';
}
else
{
if(count(getUsersWithThisEmail($_POST['email'])) > 0)
{
echo 'That email is already in use, are you sure you\'re not registered with us already? <a href="loginPage.php">Login Here</a>';
}
else
{
if (tryToCreateaUser())
{
echo 'Well done! You have successfully registered with us!';
}
else
{
echo 'There seems to be an issue getting you registered.';
}
}
}
}
else
{
echo 'Your passwords do not match, please enter them correctly.';
}
}
else
{
echo 'Your password must be between 6 to 32 characters.';
}
}

关于php - 检查 MySQL 数据库中是否存在没有唯一键的电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35703878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com