Error Insert in bank using PDO: "Only variables should be passed by reference" [duplicate]

1

I'm passing insert parameters to the database using PDO and O.O, but giving the Insert command using the $stm->bindParam(1, $usuario->getnivel()); parameters returns error saying that I should only pass variables to insert.

Follow the code below:

  public function cadastrar(Cadastro $usuario) {
        $stm = $this->pdo->prepare('INSERT INTO usuarios (nivel,username,senha) VALUES (?,?,?)');   
        $stm->bindParam(1, $usuario->getnivel());
        $stm->bindParam(2, $usuario->getusername());
        $stm->bindParam(3, $usuario->getsenha());
        $stm->execute();
}
    
asked by anonymous 26.03.2016 / 17:53

1 answer

1

bindParam() does not accept values or returns methods only accepts variables or constants, in which case it is best to change bindValue()

$stm->bindValue(1, $usuario->getnivel());
$stm->bindValue(2, $usuario->getusername());
$stm->bindValue(3, $usuario->getsenha());

More details at: What's the difference between bindParam and bindValue?

    
26.03.2016 / 18:00