Doubt in PHP update method

0

I have the following method for update

public function update($params, $id)
{
    $params_fields = "'".implode("'= ?, '", array_keys($params))."'= ?";
    $query  = "UPDATE '{$this->table}' SET {$params_fields}, WHERE 'id'=:id";
    $stmt   = $this->db->prepare($query);
    $stmt->bindValue(":id", $id);

    // Aqui esta a dúvida
    $stmt->bindValue(?,$value);

    //$stmt->execute();
    return $query;
}

I get $ params dynamically from an array, how can I go through this array by having it fill the bindValue according to the received array Ex:

for, while ou foreach{
    // resultado...
    $stmt->bindValue(1,$value);
    $stmt->bindValue(2,$value);
}
    
asked by anonymous 22.04.2017 / 14:34

1 answer

2

Do you need to get the values and fill in place in $value ? Try this:

<?php
$i = 1;
$array = array('valor1', 'valor2', 'valor3');

foreach($array as $valores){

    $stmt->bindValue($i, $valores);
    $i++;
}
?>
    
22.04.2017 / 14:38