title: creating a flexible update query with Php and pdo - problems with bindparam
body: <p>I'm updating my mysql functions to use PDO. I've got the hang of most of it but struggling with an update function to update multiple fields in a records.
  The function is in a class and I'm trying to keep it flexible to reuse with other tables etc.</p>
  
  <p>Here's my function so far:</p>
  
  <pre><code> public function dbUpdateRecord($table, $values, $where)
  {
  
  
      $this-&gt;conn();
      $fieldNames = array_keys($values);
      var_dump($fieldNames);
      $set="";
      foreach ($fieldNames as $field) {
          $set .= " $field = :$field,";
      }
  
      //strip last comma
      $set = substr($set, 0, strlen($set) - 1);
  
  
      $wherefields = array_keys($where);
      $whereCondition="";
      foreach ($wherefields as $field) {
          $whereCondition .= " $field = :$field AND";
      }
      //strip last AND
      $whereCondition = substr($whereCondition, 0, strlen($whereCondition) - 3);
  
      $sql = "UPDATE $table SET $set WHERE $whereCondition";
      var_dump($sql);
  
      $stmt = $this-&gt;db-&gt;prepare($sql);
  
      foreach ($values as $field =&gt; $value) {
  
          $stmt-&gt;bindParam(':$field', $value);
      }
      foreach ($where as $field =&gt; $value) {
          $stmt-&gt;bindParam(':$field', $value);
      }
      return $stmt-&gt;execute();
  }
  </code></pre>
  
  <p>The problem is all the fields in the record are being updated by the id of the record which is contained in the $where variable.</p>
  
  <p>$values contains an array of (fieldname=>value).</p>
  
  <p>I think the problem lies around the bindparam and trying to make the fieldnames/placeholders dynamic</p>
  
  <p>I thought I needed to use bindparam as best practice - is this correct or can I just go to execute()?</p>
  
  <p>ANy help appreciated</p>