2

My register form was working properly, than in my index.php I changed all mysql functions to mysqli functions and now it does not work anymore. Register.php code:

<?php
require('config.php');
session_start();
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
    $username = htmlEntities($_POST['username'],ENT_QUOTES);
    $password = htmlEntities($_POST['password'],ENT_QUOTES);

    $query = "INSERT INTO `users` (username, password) VALUES ('$username', '$password')";
    $result = mysqli_query($connect, $query);
    if($result){
       header("Location:index.php");
    }
    else
    {
        echo "Het werkt niet!";
    }    
}
?>
jarlh
  • 42,561
  • 8
  • 45
  • 63

1 Answers1

0

Don't know the contents of you config file so added the information here.

<?php
if ( isset( $_POST[ 'username' ] ) && isset( $_POST[ 'password' ] ) )
{
    $username = htmlEntities( $_POST[ 'username' ], ENT_QUOTES );
    $hashed_password = password_hash( $_POST[ 'password' ], PASSWORD_BCRYPT, [ 'cost' => 12 ] ); // hashes the password before inserting into the database, do not alter the password though.

    $servername = "localhost";
    $username   = "username";
    $password   = "password";
    $dbname     = "myDB";

    // Create connection
    $conn = new mysqli( $servername, $username, $hashed_password, $dbname );

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // prepare and bind
    $stmt = $conn->prepare( "INSERT INTO users ( username, password ) VALUES ( ?, ? ) " );
    $stmt->bind_param( "ss", $username, $password );

    $stmt->execute();

}
?>

reference - http://www.w3schools.com/php/php_mysql_prepared_statements.asp

I have replaced the password line with one that will hash the password before placing it into the database.

When you come to check the password in the future use:

if( password_verify( $supplied_password, $hashed_password ) )
{
  // Passed
}
Blinkydamo
  • 1,582
  • 9
  • 20
  • `$stmt->bind_param( "sss", $username, $password );` you've got one to many `s` there ;-) – Qirel Nov 17 '16 at 12:24