2

I am getting the error: Cannot pass parameter 2 by reference in.....

in this line...

$stmt1->bindParam(':value', $_SESSION['quantity'.$i] * $_SESSION['price'.$i], PDO::PARAM_STR);   

What is wrong with code above ??

user3790186
  • 239
  • 6
  • 21
  • Possible duplicate of [PHP error: "Cannot pass parameter 2 by reference"](https://stackoverflow.com/questions/13105373/php-error-cannot-pass-parameter-2-by-reference) – Cees Timmerman Sep 22 '17 at 08:40

2 Answers2

7

I'd say it's the typical confusion between PDO:bindParam() and what you probably intended to use: PDO:bindValue().

PDOStatement::bindParam

Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

PDOStatement::bindValue

Binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
5

It expects the second paramter to be a variable which can be passed by reference. Assuming $stmt1is a PDO statement then, as the docs for bindparam say

Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

Your second param is an expression ($_SESSION['quantity'.$i] * $_SESSION['price'.$i]) not a variable. Since you appear to want to evaluate the exptression now, I guess you should used bindValue() instead.

Adam
  • 6,539
  • 3
  • 39
  • 65