I am following this tutorial: https://youtu.be/gCo6JqGMi30 to make a login system using PHP and mySQL database.
However, when I try to signup to the “website” i have created, it throws this error:
Fatal error: Uncaught Error: Call to undefined function emptyInputSignup() in C:xampphtdocsPROJECTS-2022login-system-20-6-22includessignup.inc.php:24 Stack trace: #0 {main} thrown in C:xampphtdocsPROJECTS-2022login-system-20-6-22includessignup.inc.php on line 24
I have 2 seperate files needed for the signup process:
signup.inc.php (the file that calls some functions from the file functions.inc.php)
<?php
//If the user actully submitted the form the proper way then we want to run the php code inside this file here.
if (isset($_POST["submit"])) {
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdRepeat = $_POST["pwdrepeat"];
/*ERROR HANDLERS*/
/*1st type of error: If the user left any inputs empty (forgot to put username, passowrd or name etc.)*/
if (emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=emptyinput"); // "error=emptyinput" : the error message
exit(); //it is going to stop the script from running entirely
}
/*2nd type of error: If the user has sybmitted a username that is not proper*/
if (invalidUid($username) !== false) {
header("location: ../signup.php?error=invalidUid");
exit();
}
/*3rd type of error: If the user has submited an email that is not proper (e.g forgot the @ symbol)*/
if (invalidEmail($email) !== false) {
header("location: ../signup.php?error=invalidEmail");
exit();
}
/*4th type of error: If the repeated password the user has submited, doesnt match the original password)*/
if (pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=passwordsdontmatch");
exit();
}
/*5th type of error: If the username and email already exists in the database (the user tries to signup with a username that is already taken) */
if (uidExists($conn, $username, $email) !== false) {
header("location: ../signup.php?error=usernametaken");
exit();
}
//to this point, the user made no mistakes so we are gonna sign the user into the website:
createUser($conn, $name, $email, $username, $pwd);
}
//If the user didnt get to this signup.inc.php page the proper way then we want to send them back to the signup.php page.
else {
header("location: ../signup.php");
exit();
}
functions.inc.php (the file that contains the functions)
<? php
/* 1ST TYPE OF ERROR AT THE signup.inc.php FILE */
function emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) {
$result;
//if ANY of these are empty:
if (empty($name) || empty($email) || empty($username) || empty($pwd) || empty($pwdRepeat)) {
$result = true;
}
//if there are no empty fields:
else {
$result = false;
}
return $result;
}
/*2ND TYPE OF ERROR AT THE signup.inc.php FILE */
function invalidUid($username) {
$result;
//If the username is invalid:
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$result = true;
}
//if the username is valid:
else {
$result = false;
}
return $result;
}
/*3RD TYPE OF ERROR AT THE signup.inc.php FILE */
function invalidEmail($email) {
$result;
//if the email is not proper:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$result = true;
}
//if the email is proper:
else {
$result = false;
}
return $result;
}
/*4TH TYPE OF ERROR AT THE signup.inc.php FILE */
function pwdMatch($pwd, $pwdRepeat) {
$result;
if ($pwd !== $pwdRepeat) {
//if the password doesnt match the repeated password:
$result = true;
}
//if the password matched the repeated password:
else {
$result = false;
}
return $result;
}
/*5TH TYPE OF ERROR AT THE signup.inc.php FILE */
function uidExists($conn, $username, $email) {
//we check if the username and the email as well have been taken
$sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;"; // SQL STATEMENT
$stmt = mysqli_stmt_init($conn); //PREPARED STATEMENT
if (!mysqli_stmt_prepare($stmt, $sql)) {
//if the prepared statement fails it sends the user back to the signup page:
header("location: ../signup.php?error=stmttaken");
exit();
}
//if the prepared statement succeeds then we pass the data from the user
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
//EXECUTE THE PREPARED STATEMENT
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt); //RESULT STATEMENT
//checking if there is a result from the results statement. See if anything uses $resultData
//If we get results from the database it returns as true, if not it returns as false
if ($row = mysqli_fetch_assoc($resultData)) {
//return all the data from the database if this user exists inside the database:
return $row;
} else {
$result = false;
return $resut;
}
mysqli_stmt_close($stmt);
}
//FUNCTION TO SIGN THE USER UP
function createUser($conn, $name, $email, $username, $pwd) {
$sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd) VALUES (?, ?, ?, ?);"; //SQL STATEMENT
$stmt = mysqli_stmt_init($conn); //PREPARED STATEMENT
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../signup.php?error=stmtfailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT); //include the password submitted by the user which is $pwd and then hash it
//include the $hashedPwd instead of the $pwd:
mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $username, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../signup.php?error=none");
exit();
}
It says that that the error is on this line at the signup.inc.php:
if (emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) !== false) {
But I can’t find a solution. I have included the functions.inc.php inside the signup.inc.php file and I don’t seem to have forgotten any closing bracket on the functions.
