'Sign-Up Form Working But Errors Not Showing
I made a sign-up form for my website using php on my webpage, and connecting it to mySQL. The database seems to work and the data for signed up users does appear in it. However, on the front-end no messages of "Registration successful" or any errors are being displayed such as "Email already registered" But the queries seem to work as it only registers accounts following the conditions. I would appreciate any help in getting the messages to work.
Attached is the code in question:
Sign-up form:
<?php
require_once "config.php";
require_once "session.php";
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
$fullname = trim($_POST['name']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$confirm_password = trim($_POST['confirm_password']);
$password_hash = password_hash($password, PASSWORD_BCRYPT);
if($query = $db->prepare("SELECT * FROM users WHERE email = ?")) {
$error = '';
//Bind parameters (s=string, i=int, b=blob, etc), in our case the username is a string so we use "s"
$query->bind_param('s', $email);
$query->execute();
//Store the result so we can check if account exists in database
$query->store_result();
if ($query->num_rows > 0) {
$error .= '<p class="error">The email address is already registered!</p>';
} else {
//Validate password
if (strlen($password) < 6) {
$error .= '<p class="error">Password must have atleast 6 characters</p>';
}
//Validate confirm password
if (empty($confirm_password)) {
$error .= '<p class="error">Please enter confirm password.</p>';
} else {
if (empty($error) && ($password != $confirm_password)) {
$error .= '<p class="error">Passwords did not match</p>';
}
}
if (empty($error)) {
$insertQuery = $db->prepare("INSERT INTO users (name, email, password) VALUES (?,?,?);");
$insertQuery->bind_param("sss", $fullname, $email, $password_hash);
$result = $insertQuery->execute();
if ($result) {
$error .= '<p class="success">Your registration was successful</p>';
} else {
$error .= '<p class="error">Something went wrong!</p>';
}
$insertQuery->close();
}
}
}
$query->close();
//Close DB connection
mysqli_close($db);
}
?>
config.php:
<?php
define('DBSERVER', 'localhost'); //Database server
define('DBUSERNAME', 'root'); //Database username
define('DBPASSWORD',''); //Database password
define('DBNAME', 'users'); //Database name
/* connect to MySQL databse */
$db = mysqli_connect(DBSERVER, DBUSERNAME, DBPASSWORD, DBNAME);
//check db connection
if($db === false){
die("Error: connection error.".mysqli_connect_error());
}
?>
session.php:
<?php
//Start the session
session_start();
//if the user is already logged in then redirect user to welcome page
if (isset($_SESSION["userid"]) && $_SESSION["userid"] === true) {
header("location: loggedinuserhome.html");
exit;
}
?>
I am making this form following tutorials so lack deep knowledge of the subject so would love if someone could guide me through it and explicitly point out the error. Thank You!
Solution 1:[1]
You have stored your messages in $error but you haven't displayed them. Display them according to the condition using echo.
echo $error;
Solution 2:[2]
Your Registration Successful message is also stored in $error variable but where you print $error, that is missing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sagar |
| Solution 2 | Isha |
