My database is not adding users when I signup

Jonas Schou

This is my HTML file:

 <div class="form">
            <form action="register.php" method="POST" class="register-form">
            <input type="text" placeholder="Username" name="username" required/>
            <input type="password" placeholder="Password" name="password" required/>
            <input type="text" placeholder="Email" name="email"  required/>
            <button type="submit">Create</button>
            <p class="message"> Already Registered? <a href="#">Login</a>
            </p>
            </form>
    
            <form action="login.php" method="POST" class="login-form">
            <input type="text" placeholder="Username" name="username" required/>
            <input type="password" placeholder="Password" name="password" required/>
            <button type="submit">login</button>
            <p class="message">Not Registered? <a href="#">Register</a></p>
            </form>

This is my PHP file:

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

if (!empty($username) || !empty($password) || !empty($email)) {
 $serverName = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbname = "account";

    //create connection
    $conn = new MySQLI($serverName,$dbUsername,$dbPassword,$dbname);
    if (mysqli_connect_error()) {
     die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
    } else {
     $SELECT = "SELECT email From users Where email = ? Limit 1";
     $INSERT = "INSERT Into users (username, password, email) values(?, ?, ?)";

     //Prepare statement
     $stmt = $conn->prepare($SELECT);
     $stmt->bind_param("s", $email);
     $stmt->execute();
     $stmt->bind_result($email);
     $stmt->store_result();
     $stmt->store_result();
     $stmt->fetch();
     $rnum = $stmt->num_rows;
     if ($rnum==0) {
      $stmt->close();
      $stmt = $conn->prepare($INSERT);
      $stmt->bind_param("sss", $username, $password, $email);
      $stmt->execute();
      echo "New record inserted sucessfully";
     } else {
      echo "Someone already register using this email";
     }
     $stmt->close();
     $conn->close();
    }
} else {
 echo "All field are required";
 die();
}

I have a database called account, with a table called users, columns called id, email, username & password. The ID is an INT, and selected as primary. And the rest is set as VARCHAR.

When I enter some names in the form, and press signup, it's giving me the result "New record inserted successfully", so I have no idea, why this doesn't work.

Dharman

Your problem is the way you use mysqli. As I have said in the comments mysqli is not suitable for beginners, the API is very cumbersome.

Look at the lines before your INSERT statement. You perform a SELECT statement, presumably to check if the email has been used before and then you bind the result variable. The variable is called $email. You overwrite your user input with the result from SELECT. But this is not the right way.

The simple solution would be to name the variable something else, but the right answer is that you should fetch a count from the SQL not the value. See adjusted code below:

<?php

$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
$email = filter_input(INPUT_POST, 'email');

if ($username && $password && $email) {
    $serverName = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbname = "account";

    //create connection
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $conn = new MySQLI($serverName, $dbUsername, $dbPassword, $dbname);
    $conn->set_charset('utf8mb4'); // always set the charset

    //Prepare statement
    $stmt = $conn->prepare("SELECT COUNT(email) From users Where email = ? Limit 1");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($exists); // we fetch the count
    $stmt->fetch();
    if (!$exists) {
        $stmt = $conn->prepare("INSERT Into users (username, password, email) values(?, ?, ?)");
        // Don't forget to hash the password and never store the real password anywhere
        $hash = password_hash($password, PASSWORD_DEFAULT);
        $stmt->bind_param("sss", $username, $hash, $email);
        $stmt->execute();
        echo "New record inserted sucessfully";
    } else {
        echo "Someone already register using this email";
    }
} else {
    echo "All field are required";
}

I remove the unnecessary code and removed the store_result() and num_rows. They are not helpful in this situation. Instead fetch a count of matching rows and check if the count is not 0 with if(!$exists)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My application crashes when I press my signup Button

How do I allow multiple users to connect to my H2 database simultaneously?

Adding users directly from the database [UserFrosting 0.3.1]

Adding users to Firebase Database

Should my users connect directly to the database? How to connect users to database;

Trouble adding users to database using PHP

Not sure why my login is not working when not combined with a prior signup

when i import the model from my database, i want the instance to my database not to be generated in the database model

SQLite Importer will overwrite my database when I load my application?

Error when adding an entity to my SQL database when using javax.persistence.EntityManager

How can I identify users of my API when they call it in JS?

What should be my path when I want users to upload file on my website?

When I update my Avatar picture, it changes all users picture

Restriction for not allowing same email and password in the database for my signup form

What l am going wrong when in my signup application

Using PHP classes when adding users to database

When I click delete on a single user it deletes all my users in my database | Android

How do I get my user info to save to the database from the Signup form?

I created quiz in PHP it working but in signup database not updating

how can i get username when he/she signup in django?

I cant get the expected output in my code (signup login system)

C#: I'm getting a "MySqlException: Cannot add or update a child row: a foreign key constraint fails" when i am Adding an object in my database

How do I connect to my Heroku postgreSQL database to authenticate users?

How do I check users email isn't already in my database when submitting a form?

How can I write a firebase rule for my database so that it prevents certain users from being able to write?

When I start my spring boot application, my database is not initiating

How I can take/save a js signup to my json file

i want redirection to be done to account created when signup form is submitted

I got the certificate error when I want to migrate my database