2

I'm practicing building a PHP registration form script for a website. I have done this code, but when I click the submit button I get the notice: Only variables should be passed by reference in line 13, and I'm stuck on what to do here. Any help is greatly appreciated, again I'm not a PHP expert.

<?php

require 'database.php';

if(!empty($_POST['email']) && !empty($_POST['username']) && !empty($_POST['password'])):

    //Enter the new user into the database
    $sql = "INSERT INTO users (email, username, password) VALUES (:email, :username, :password)";
    $stmt = $conn->prepare($sql);

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

    if($stmt->execute() ):
        die('Success');
    else: 
        die('Fail');
    endif;  
endif;

?>

here

Milad
  • 27,506
  • 11
  • 76
  • 85
Stephen M
  • 31
  • 6
  • 1
    Good reference for when you get php errors http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php –  Jul 23 '16 at 05:31

2 Answers2

4

change this

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

to this

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

$stmt->bindParam(':email', $email);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password',$password);

the error message is clear though you need to assign those values into variables then pass them

Ahmad ghoneim
  • 844
  • 7
  • 13
0

$result->bindParam(':id', $id, PDO::PARAM_INT);

if string, you need write:
$stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR);

Taron
  • 169
  • 1
  • 13