Problem with: "Only variables should be passed by reference in"

0

I'm starting to learn to program in PHP, and right now I'm doing a simple web. As it says in the title, I have the error of: "Only variables should be passed by reference in". Which is not an error since the code does what I want it to do (which is to add users to my DB).

The code that produces the error is the following:

$stmt -> bindParam(':nombre', $usuario -> obtener_nombre(), PDO::PARAM_STR);
$stmt -> bindParam(':email', $usuario -> obtener_nombre(), PDO::PARAM_STR);
$stmt -> bindParam(':password', $usuario -> obtener_nombre(), PDO::PARAM_STR);

Who, searching the Internet, found a possible solution, which is:

$nombreUser = $usuario -> obtener_nombre();
$$sentencia -> bindParam(':nombre', $nombreUser, PDO::PARAM_STR);

$emailUser = $usuario -> obtener_nombre();
$sentencia -> bindParam(':email', $emailUser, PDO::PARAM_STR);

$passwordUser = $usuario -> obtener_nombre();
$sentencia -> bindParam(':password', $passwordUser, PDO::PARAM_STR);

But now another problem arose, when changing the value ': name' to ': email', ': password', every time I register a new user, the values email and password take the values that are named .

I understand that the problem is in the solution I found, since I am telling my code that the value that $ user takes will be added to email and password.

Some solution to this problem

    
asked by Jonas Mor. 23.06.2018 в 01:57
source

1 answer

1

First, the problem by which email and password take the value of name, is because in all sentences of bindParam() you are using $usuario -> obtener_nombre() and in reality it should be $usuario -> obtener_mail() and $usuario -> obtener_password() correspondingly (assuming that those are the methods that call the corresponding data). The problem is that you are passing the same value to all fields.

On the other hand, the main error is because you use bindParam() instead of bindValue() . This last method allows to pass variables AND values, while bindParam() only accepts variables.

  • $nombreUser is a variable, represents a value per reference .
  • $usuario -> obtener_nombre() is a method that returns a value .

I hope I help you!

    
answered by 27.06.2018 в 16:30