为什么我一直在获取用户名?
Why do I keep on getting username is take?
我一直收到用户名被占用的消息,而我的数据库是空的..
这是我的函数文件,我在那里写的所有内容
function invalidUid($username) {
$result;
if (!preg_match("/^[a-zA-Z0-9_]+$/", $username)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function uidExists($conn, $username, $email) {
$sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) { //creating a variable while checking if its true or false
return $row;
}
else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
function createUser($conn, $name, $email, $username, $pwd) { //this will create the USER or data into the database
$sql = "INSERT INTO users (usersName, usersEmail, usersUid. usersPwd) VALUES (?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=stmtproblemo");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $username, $pwd, $hashedPwd); //the ss is how many data you have like for insert we have 4 now so 4 S
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../signup.php?error=none");
exit();
}
这是我注册认证的部分代码
if (isset($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdRepeat = $_POST["pwdrepeat"];
if (invalidUid($username) !== false) {
header("Location: ../index.php?error=invaliduid");
exit();
}
if (invalidEmail($email) !== false) {
header("Location: ../index.php?error=invalidemail");
exit();
}
if (pswMatch($pwd, $pwdRepeat) !== false) {
header("Location: ../index.php?error=passworddoesntmatch");
exit();
}
if (uidExists($conn, $username, $email) !== false) {
header("location: ../index.php?error=usernametaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
}
else {
header("Location: ../index.php");
exit();
}
我做错了什么?我试图添加 (!preg_match("/^[a-zA-Z0-9_]+$/",$_POST['username'] 但它给了我 invalidUid。如果你有对此代码其他部分的任何其他评论请告诉我。
首先思考必须发生的事情。如果您收到 'username already taken' 错误,这意味着您的 uidExists()
函数是要查看的函数,并且它不是预期的 returning false
。那么,单步执行该代码,怎么会发生这种情况?
if ($row = mysqli_fetch_assoc($resultData)) {
如果没有匹配的行,这里会发生什么? Checking the docs(强调我的):
Return Values.
Returns an associative array representing the fetched row, where each key in the array represents the name of one of the result set's columns, null if there are no more rows in the result set, or false on failure.
所以 $row
将是 null
,在这种情况下 uidExists()
returns null
。但是您的代码没有测试 null
:
if (uidExists($conn, $username, $email) !== false) {
此测试将始终评估为 true
,因为 null
不是 false
,因此您始终会获取用户名。
最快的解决方法是更改测试以检查 null
,这就是没有匹配记录时得到的结果:
if (uidExists($conn, $username, $email) !== null) {
但请记住,这意味着您没有错误检查 - 如果您的查询被破坏或出现问题,您将永远不会知道。您应该考虑为查询错误添加单独的检查,并对它们采取一些措施,例如记录它们或 return 向您的用户发送错误。
我一直收到用户名被占用的消息,而我的数据库是空的..
这是我的函数文件,我在那里写的所有内容
function invalidUid($username) {
$result;
if (!preg_match("/^[a-zA-Z0-9_]+$/", $username)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function uidExists($conn, $username, $email) {
$sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) { //creating a variable while checking if its true or false
return $row;
}
else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
function createUser($conn, $name, $email, $username, $pwd) { //this will create the USER or data into the database
$sql = "INSERT INTO users (usersName, usersEmail, usersUid. usersPwd) VALUES (?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=stmtproblemo");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $username, $pwd, $hashedPwd); //the ss is how many data you have like for insert we have 4 now so 4 S
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../signup.php?error=none");
exit();
}
这是我注册认证的部分代码
if (isset($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdRepeat = $_POST["pwdrepeat"];
if (invalidUid($username) !== false) {
header("Location: ../index.php?error=invaliduid");
exit();
}
if (invalidEmail($email) !== false) {
header("Location: ../index.php?error=invalidemail");
exit();
}
if (pswMatch($pwd, $pwdRepeat) !== false) {
header("Location: ../index.php?error=passworddoesntmatch");
exit();
}
if (uidExists($conn, $username, $email) !== false) {
header("location: ../index.php?error=usernametaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
}
else {
header("Location: ../index.php");
exit();
}
我做错了什么?我试图添加 (!preg_match("/^[a-zA-Z0-9_]+$/",$_POST['username'] 但它给了我 invalidUid。如果你有对此代码其他部分的任何其他评论请告诉我。
首先思考必须发生的事情。如果您收到 'username already taken' 错误,这意味着您的 uidExists()
函数是要查看的函数,并且它不是预期的 returning false
。那么,单步执行该代码,怎么会发生这种情况?
if ($row = mysqli_fetch_assoc($resultData)) {
如果没有匹配的行,这里会发生什么? Checking the docs(强调我的):
Return Values.
Returns an associative array representing the fetched row, where each key in the array represents the name of one of the result set's columns, null if there are no more rows in the result set, or false on failure.
所以 $row
将是 null
,在这种情况下 uidExists()
returns null
。但是您的代码没有测试 null
:
if (uidExists($conn, $username, $email) !== false) {
此测试将始终评估为 true
,因为 null
不是 false
,因此您始终会获取用户名。
最快的解决方法是更改测试以检查 null
,这就是没有匹配记录时得到的结果:
if (uidExists($conn, $username, $email) !== null) {
但请记住,这意味着您没有错误检查 - 如果您的查询被破坏或出现问题,您将永远不会知道。您应该考虑为查询错误添加单独的检查,并对它们采取一些措施,例如记录它们或 return 向您的用户发送错误。