Hi there, I was creating error messages for my signup page if a username already exist, the email already exists etc. However, I always get a success message even if the new user has the same email address as another user = several users with the same email. I have looked inside my code, but I can’t find the problem, please help.
- Thank you
**sql table:**
CREATE TABLE users(
id INTEGER AUTO_INCREMENT PRIMARY KEY NOT NULL,
uidUsers VARCHAR(32) NOT NULL,
emailUsers VARCHAR(255) NOT NULL,
pwdUsers VARCHAR(255) NOT NULL,
nameUsers VARCHAR(255) NOT NULL
);
signup.php:
<link rel="stylesheet" type="text/css" href="login.css">
<link rel="icon" type="image/png" href="favicon.png">
<main>
<?php
//bruker get fordi vi henter informasjon fra url
if (isset($_GET['error'])) {
if ($_GET['error'] == "emptyfields") {
echo '<p class="signuperror"> Fill in all fields!</p>';
}
else if ($_GET['error'] == "invaliduidmail") {
echo '<p class="signuperror"> Invalid username and e-mail!</p>';
}
else if ($_GET['error'] == "invaliduid") {
echo '<p class="signuperror"> Invalid username!</p>';
}
else if ($_GET['error'] == "invalidmail") {
echo '<p class="signuperror"> Invalid e-mail!</p>';
}
else if ($_GET['error'] == "passwordcheck") {
echo '<p class="signuperror"> Your passwords do not match!</p>';
}
else if ($_GET['error'] == "usertaken") {
echo '<p class="signuperror"> Username is already taken!</p>';
}
}
else if ($_GET['signup'] == "success") {
echo '<p class="signupsuccess"> Signup successful!</p>';
}
?>
</main>
<body>
<div class="login-box">
<h1> Sign up </h1>
<div class="textbox">
<form action="includes/signup.inc.php" method="post">
<input type="text" name="uid" placeholder="Username">
</div>
<div class="textbox">
<input type="text" name="mail" placeholder="E-mail">
</div>
<div class="textbox">
<input type="password" name="pwd" placeholder="Password">
</div>
<div class="textbox">
<input type="password" name="pwd-repeat" placeholder="Repeat password">
</div>
<button type="submit" name="signup-submit"> Signup </button>
</form>
</div>
</body>
**signup.inc.php:**
<?php
if (isset($_POST['signup-submit'])) {
require "dbhandler.inc.php";
$username = $_POST['uid'];
$email = $_POST['mail'];
$password = $_POST['pwd'];
$passwordRepeat = $_POST['pwd-repeat'];
//Check if ANY columns is left empty while signing in, and keeping information that is already typed in before entering "sign up"
if (empty($username) || empty($email) || empty($password) || empty($passwordRepeat)) {
header("Location: ../signup.php?error=emptyfields&uid=".$username."&mail=".$email);
exit();
}
else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)) {
header("Location: ../signup.php?error=invalidmailuid");
exit();
}
else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidmail&uid=".$username);
exit();
}
else if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
header("Location: ../signup.php?error=invaliduid&mail=".$email);
exit();
}
else if ($password !== $passwordRepeat) {
header("Location: ../signup.php?error=passwordcheckuid=".$username. "&mail=".$email);
exit();
}
else {
$sql = "SELECT uidUsers FROM users WHERE uidUsers=?";
$statement = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($statement, $sql)) {
header("Location: ../signup.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
$resultCheck = mysqli_stmt_num_rows($statement);
if ($resultCheck > 0) {
header("Location: ../signup.php?error=usertaken&mail=".$email);
exit();
}
else {
$sql = "INSERT INTO users (uidUsers, emailUsers, pwdUsers) VALUES (?, ?, ?)";
$statement = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($statement, $sql)) {
header("Location: ../signup.php?error=sqlerror");
exit();
}
else { //bcrypt = latest version of hashing
$hashedpwd = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($statement, "sss", $username, $email, $hashedpwd);
mysqli_stmt_execute($statement);
header("Location: ../login.php?signup=success");
exit();
}
}
}
}
//Close connection
mysqli_stmt_close($statement);
mysqli_close($conn);
}
else {
header("Location: ../signup.php");
exit();
}
Relevant files