Hello everyone!
I have a code for a login system in php with mysqli here, but I keep getting the empty field error even though all fields are filled in. I’ve spent days with it, writing, correcting, overwriting, and rewriting the entire thing with no luck. The error message written below. The user is not added to the database.
What am I doing wrong? I’m fairly new to programming. All and any help is much appreciated!
The error:
http:/localhost…/index.php?error=emptyfields&uid=User%20Name&[email protected]&date=2020-09-10&country=Denmark&tel=11111111
The code for signup system is here:
<?php
if (isset($_POST['signup-submit'])) {
require 'dbh.inc.php';
$username = $_POST['uid'];
$email = $_POST['mail'];
$password = $_POST['pwd'];
$passwordRepeat = $_POST['pwd-repeat'];
$birthDate = $_POST['date'];
$country = $_POST['country'];
$telephone = $_POST['tel'];
if (empty($username) || empty($email) || empty($password) || empty($passwordRepeat) || empty($birthDate) || empty($country) || empty($telephone)) {
header("Location: ../index.php?error=emptyfields&uid=".$username."&mail=".$email."&date=".$birthDate."&country=".$country."&tel=".$telephone);
exit();
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA_Z0-9]*$/", $username)){
header("Location: ../index.php?error=invaliduidmail=");
exit();
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)){
header("Location: ../index.php?error=invalidmail&uid=".$username);
exit();
}
elseif (!preg_match("/^[a-zA_Z0-9]*$/", $username)){
header("Location: ../index.php?error=invaliduid&mail=".$email);
exit();
}
elseif ($password !== $passwordRepeat){
header("Location: ../index.php?error=passwordcheck&mail=".$email."&uid=".$username);
exit();
}
elseif (!preg_match("/^[a-zA_Z0-9]*$/", $birthDate)){
header("Location: ../index.php?error=invaliddate&mail=".$email);
exit();
}
elseif (!preg_match("/^[a-zA_Z]*$/", $country)){
header("Location: ../index.php?error=invalidcountry&mail=".$email);
exit();
}
elseif (!preg_match("/^[0-9]*$/", $telephone)){
header("Location: ../index.php?error=invalidtel&mail=".$email);
exit();
}
else {
$sql = "SELECT emailUsers FROM users WHERE emailUsers=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if ($resultCheck > 0) {
header("Location: ../index.php?error=usertaken&uid=".$username);
exit();
}
else{
$sql = "INSERT INTO users (uidUsers, emailUsers, pwdUsers, birthUsers, countryUsers, telUsers) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=sqlerror");
exit();
}
else {
$hashedpwd = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssssss", $username, $email, $hashedpwd, $birthDate, $country, $telephone);
mysqli_stmt_execute($stmt);
header("Location: ../index.php?signup=success");
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else{
header("Location: ../index.php");
exit();
}
The code for the signup form is here:
<form class="frm" action="includes/signup.inc.php" method=post>
<input type="text" name="uid" placeholder="Name">
<input type="text" name="mail" placeholder="E-mail address">
<input type="password" id="pwd" placeholder="Password">
<input type="password" id="pwd-repeat" placeholder="Repeat password">
<input type="date" name="date" placeholder="dd/mm/yyyy">
<input type="text" name="country" placeholder="Country">
<input type="tel" name="tel" placeholder="Telephone number">
<button class="btn2" type"submit" name="signup-submit">Join us</button>
</form>