This question already has an answer here:
- Reference - What does this error mean in PHP? 34 answers
I am building this PHP login system to work on my skills. When the user signs up and their information is secured in the SQL database, I want to display signup.php?signup=success on top of my screen. When the user types in the information and something seems to be missing, I want it to display signup.php?signup=empty.
However, on my current page, even when I type in information, It keeps on displaying me signup.php?signup=empty. I know that my database is working for sure though....
Here is my PHP code for my signup.inc.php (included in an includes folder):
<?php
if (isset($_POST['submit'])) {
include_once 'dbh.php';
$first = mysqli_real_escape_string($conn, $_POST['first']);
$last = mysqli_real_escape_string($conn, $_POST['last']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
//Error handlers
//Check for empty fields
if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
header("Location: ../signup.php?signup=empty");
exit();
} else {
//Check if input characters are valid
if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
header("Location: ../signup.php?signup=invalid");
exit();
} else {
//Check if email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?signup=email");
exit();
} else {
$sql = "SELECT * FROM users WHERE user_uid='$uid'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: ../signup.php?signup=usertaken");
exit();
} else {
//Hashing the password
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
//Insert the user into the database
$sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
mysqli_query($conn, $sql);
header("Location: ../signup.php?signup=success");
exit();
}
}
}
}
} else {
header("Location: ../signup.php");
exit();
}
Here is my signup.php form (I included header.php that has the database) :
<?php
include 'header.php';
?>
<section class="main-container">
<div class="main-wrapper">
<h2>Signup</h2>
<form class="signup-form" action="includes/signup.inc.php" method="POST">
<input type="text" name="first" placeholder="First Name">
<input type="text" name="last" placeholder="Last Name">
<input type="text" name="email" placeholder="Email">
<input type="text" name="uid" placeholder="Username">
<input type="password" name="password" placeholder="password">
<button type="submit" name="submit">Sign Up</button>
</form>
</div>
</section>
<?php
include 'footer.php';
?>
What am i doing wrong here?
I appreciate all responses