wiki
wiki

Reputation: 299

PHP PDO BindParam() not works

I have a web service that receives a JSON string with multiple places. The first thing I do is put this JSON in an array, which I have checked and all its elements are correct.

$json = file_get_contents('php://input');
$obj = json_decode($json);

Now I want to do an INSERT INTO SELECT in the table progreso of all the elements of the bcficha table whose location matches the element of the array.

To do this I make a for loop where I make the bindParam of the query with the value of each of the array elements.

$sql = $bdd->prepare("INSERT INTO progreso (progreso.id, progreso.ubicacion, progreso.nombre, progreso.evacuacion,progreso.prioridad) SELECT bcficha.id, bcficha.ubicacion, bcficha.nombre,bcficha.evacuacion, bcficha.prioridad FROM bcficha WHERE bcficha.ubicacion = :lugar");
    
foreach ( $obj as $sala )
{ 
     $sql->bindParam(':lugar', $sala);
        //$sql->bindValue(':lugar',$sala, PDO::PARAM_STR);
     $sql->execute();
         
     echo $sql->queryString."\n";
}

As many queries are executed as there are elements in the array, but the problem is that the bindParam is not working, and therefore the query is executed with :lugar instead of the value of $sala

I put an example of the query that is executed:

INSERT INTO progreso (progreso.id, progreso.ubicacion, progreso.nombre, progreso.evacuacion,progreso.prioridad) SELECT bcficha.id, bcficha.ubicacion, bcficha.nombre,bcficha.evacuacion, bcficha.prioridad FROM bcficha WHERE bcficha.ubicacion = :lugar

The correct query would be:

INSERT INTO progreso (progreso.id, progreso.ubicacion, progreso.nombre, progreso.evacuacion,progreso.prioridad) SELECT bcficha.id, bcficha.ubicacion, bcficha.nombre,bcficha.evacuacion, bcficha.prioridad FROM bcficha WHERE bcficha.ubicacion = sala 1

( where 'sala 1' is the first value of the array obj)

That's why I say that the bindParam is not being executed, because it does not replace :lugar with the value of the $sala variable

I have read forums that the value of the variable of the where clause must be fixed, so I think I can make an array with the queries and simply do a foreach with an execute of these queries

Why is bindParam not setting the value of $sala? How can I solve that? Thanks.

Upvotes: 0

Views: 28

Answers (0)

Related Questions