0

I'm working on a login page for a PHP database (xaamp) and this code is for the sign-up part. This code is meant to check if the database has an existing username. When there are no inputs in the database, the sign-up page works but once a submission is made no more can be made after that. I'm not too sure how to fix that. This is HTML, javascript and PHP.

 if (uidExists($conn, $username) !== false) {
    header("location: ../homepage.php?error=usernametaken");
    exit();
}

  function uidExists($conn, $username) {
    $sql = "SELECT * FROM user_information WHERE UserUid = ?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("location: ../homepage.php?error=stmtfailed");
    exit();
    }

    mysqli_stmt_bind_param($stmt, "s", $username);
    mysqli_stmt_execute($stmt);
    

    $resultData = mysqli_stmt_get_result($stmt);

    if ($row = mysqli_fetch_assoc($resultData)) {
        return $row;
    }
    else {
        $result = false;
        return $result;
    }

    mysqli_stmt_close($stmt);

}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Aioli
  • 1
  • 1
  • You should post your code, otherwise we will not be able to support you – the_smart_home_maker Aug 13 '22 at 09:37
  • I’m not sure on your specific problem, but would you accept some general critique and cleanup on your code? https://3v4l.org/EKkqG#v8.1.9. First, use types to guard things. Second, use MySQL error reporting. You can wrap in a `try` and report your own if you want. Third, don’t redirect inside the function, only return or throw. Let the caller redirect if needed. Fourth, avoid comparing with literal `true` or `false` unless you know the function will return a “falsey” type, it is much easier to read. Fifth, “exists” in a function names implies true or false generally, so only return those. – Chris Haas Aug 13 '22 at 13:00

0 Answers0