1

I have this PHP that basically is being used for inserting an email and password into an SQL database:

<?php  
error_reporting(E_ALL ^ E_STRICT);
require "database.php";
$message = '';

  if (!empty($_POST["email"]) &&!empty($_POST["password"])):
  //Enter the new user in the database
    $sql = "INSERT INTO users (email, password) VALUES (:email, :password)";
    $stmt = $conn->prepare($sql);

    $stmt->bindParam(":email", $_POST['email']);
    $stmt->bindParam(":password", password_hash($_POST['password'], PASSWORD_BCRYPT));

    if ($stmt->execute() ):

      $message = 'Successfully created a new user';
    else:
      $message = 'Sorry there must have been an issue whilst registering';
    endif;

  endif;
?>

Here is the form:

<div class="jumbotron" id="jumbotron-6">
  <div class="container text-center">
  <?php if (!empty($message)):
  ?>
  <h3 id="h3message"><?= $message ?> </h3>
<?php endif; ?>
     <form action="signup.php" method="POST">
      <input type="text" placeholder="enter your email" name="email"> 
        <input type="password" placeholder="and password" name="password">
          <input type="password" placeholder="confirm password" name="confirm_password">
        <input type="submit">
    </form> 
  </div>
</div>

It doesn't insert into the database (all the fields, variables are correct i think - just email and password) and it comes back with the error message that I created that says 'Sorry there must have been an issue whilst registering'

Here is the database.php file

<?php 

$server =  'localhost';
$username = "root";
$password = "";
$database = "auth";

try{
  $conn = new PDO ("mysql:host={$server};dbname={$database};" , $username, $password);
} 
catch (PDOException $e) {
  die ( "Connection failed; " . $e->getMessage());
} 

?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
EllisJ98
  • 23
  • 5

1 Answers1

0

Hash the password before you bind it:

$UserPWHash = password_hash($_POST['password'], PASSWORD_BCRYPT);

$stmt->bindParam(":password", $UserPWHash));
ioMatrix
  • 603
  • 5
  • 6
  • I changed it to: $UserPWHash = password_hash($_POST['password'], PASSWORD_BCRYPT); $stmt->bindParam(":email", $_POST['email']); $stmt->bindParam(":password", $UserPWHash)); but still doesn't work – EllisJ98 Jul 16 '16 at 19:29
  • Not hashing the password initially did not prevent the entry from saving. It raised a notice, but it didn't prevent inserting the record. – Ayo Makanjuola Jul 16 '16 at 19:29