I am building a php login system and I have I would like some help on my error handling part.
This class is where my form is set. I am getting the following error: welcome.php?error=stmtfailed.
Now this tells me that the error is occurring on the functions.php file and has something to do with my sql statements. Any guesses? Thank you for the help guys.
<div class="modal-container" id="modalContainer">
<div class="container" id="myModal">
<div class="form-container sign-up-container">
<form action="signup.inc.php" method="POST">
<h1>MMT University</h1>
<span>Se registre para começar</span>
<input type="text" name="name" placeholder="Usuário" />
<input type="email" name="email" placeholder="Email" />
<input type="text" name="uid" placeholder="Nome completo" />
<input type="password" name="pwd" placeholder="Senha"/>
<input type="password" name="pwdrepeat" placeholder="Repita a sua senha" />
<button type="submit" name="submit">Registrar</button>
</form>
</div>
<div class="form-container sign-in-container">
<form action="login.inc.php">
<h1>Acessar</h1>
<span>Acesse a MMT University</span>
<input type="email" placeholder="Email" />
<input type="password" placeholder="Senha" />
<a href="#">Esqueceu a senha?</a>
<button>Acessar</button>
</form>
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Bem vindo!</h1>
<p>Para continuar aprendendo, acesse a sua conta!</p>
<button class="ghost" id="signIn">Acessar</button>
</div>
<div class="overlay-panel overlay-right">
<h1>MMT University!</h1>
<p>Se registre para entrar na melhor universidade trading do mundo.</p>
<button class="ghost" id="signUp">Registre-se</button>
</div>
</div>
</div>
</div>
</div>
<script src="main.js"></script>
<script src="modal.js"></script>
The signup.inc.php, which is the file that has the functions that handle the error handling.
<?php
if(isset($_POST["submit"])){
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['uid'];
$pwd = $_POST['pwd'];
$pwdRepeat = $_POST['pwdrepeat'];
require_once 'db_connection.php';
require_once 'functions.php';
if (emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) !== false) {
header("location: welcome.php?error=emptyInput");
exit();
}
if (invalidUid($username) !== false) {
header("location: welcome.php?error=invalidUid");
exit();
}
if (invalidEmail($email) !== false) {
header("location: welcome.php?error=invalidEmail");
exit();
}
if (pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: welcome.php?error=passwordsdontmatch");
exit();
}
if (uidExists($conn, $username, $email) !== false) {
header("location: welcome.php?error=usernameTaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
}
else {
header("location: welcome.php");
exit();
}
Here are the functions corresponding to that file above.
<?php
function emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat){
$result;
if (empty($name) || empty($email) || empty($username) || empty($pwd) || empty($pwdRepeat)) {
$result = true;
} else {
$result = false;
}
return $result;
}
function invalidUid($username){
$result;
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$result = true;
} else {
$result = false;
}
return $result;
}
function invalidEmail($email){
$result;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$result = true;
} else {
$result = false;
}
return $result;
}
function pwdMatch($pwd, $pwdRepeat){
$result;
if ($pwd !== $pwdRepeat) {
$result = true;
} else {
$result = false;
}
return $result;
}
function uidExists($conn, $username, $email){
$sql = "SELECT * FROM users WHERE usersUid = ? OR users = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql) ) {
header("location: welcome.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)) {
return $row;
}
else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
function createUser($conn, $name, $email, $username, $pwd){
$sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd) VALUES (?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql) ) {
header("location: welcome.php?error=stmtfailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $username, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: mainpage.php?error=none");
exit();
}
users
beusersEmail
? – Barmar Oct 26 '20 at 21:51