This question already has an answer here:
<?php
require 'database.php';
if (isset($_POST['email']) && isset($_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() ):
die('Success');
else:
die('Fail');
endif;
endif;
?>
It successfully add the user and password in the database but it gives the error below
Only variables should be passed by reference in C:\xampp\htdocs\authegister.php on line 12
</div>
The format for bindParam is as follows:
public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )
Notice this part mixed &$variable
is passed by reference. To fix this, just change:
$stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
To:
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt->bindParam(':password', $password);
Since you are then passing a variable itself by reference, not just the string returned by password_hash()
.