0

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.

wiki
  • 299
  • 4
  • 16
  • You are having two queries into one prepare. – Markus Zeller Aug 14 '22 at 16:56
  • 1
    @MarkusZeller They're not, it's a single [`INSERT … SELECT` statement](https://dev.mysql.com/doc/refman/8.0/en/insert-select.html). @wiki, everything you showed us here is OK, the problem is elsewhere. What do you mean exactly by "the bindParam is not working"? – Blackhole Aug 14 '22 at 16:58
  • @Blackhole Thanks for the reply. In the statement: $sql->queryString."\n"; shown gives the actual query that the server makes to the database. This query takes :lugar, instead of $sala, which is the value that the bindParam should place in the query that is executed on the server – wiki Aug 14 '22 at 17:02
  • 2
    It's normal: `$statement->queryString` shows the raw SQL statement (see [this question](https://stackoverflow.com/questions/6491711/pdo-querystring-with-binded-data)). – Blackhole Aug 14 '22 at 17:32
  • 1
    This question shouldn't have been closed by the way, or at least the claimed duplicate has no link whatsoever with this question. – Blackhole Aug 14 '22 at 17:36

0 Answers0