Doubt in update PHP method

Asked

Viewed 18 times

0

I have the following method for updating

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 receive $params dynamically from an array, as I can traverse this array causing it to fill in the bindValue according to the received array Ex:

for, while ou foreach{
    // resultado...
    $stmt->bindValue(1,$value);
    $stmt->bindValue(2,$value);
}

1 answer

2


You need to take the values and fill in the place on $value ? Try it like this:

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

foreach($array as $valores){

    $stmt->bindValue($i, $valores);
    $i++;
}
?>
  • Opa cool guy, vlws ae helped was not understanding the logic to make this stop there.. I made some changes here and gave right vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.