title: Using Arrays in a PDO UPDATE Prepared statement
body: <p>I have this array of titles. </p>
  
  <pre><code>$first = array('title','first_name','surname','phone','email',
  'add1','add2','add3','add4','add5');
  </code></pre>
  
  <p>These are both the column titles in a database, and the names of different form items on a web page. By listing them in <code>$first</code> I can use this array to link to the database and the <code>POST</code> values created on form submission by using the following code.</p>
  
  <pre><code>$sql = "INSERT INTO first_page_data (" . implode(',',$first) . ") VALUES (" . implode(',', array_fill(0, count($first), '?')) . ")";
  $stmt = $db-&gt;prepare($sql);
  foreach($first as $key=&gt;$val){
      $stmt-&gt;bindValue($key+1, $_POST[$val], PDO::PARAM_STR);
  
  $stmt-&gt;execute();
  </code></pre>
  
  <p>This code will create a valid SQL query using the <code>$first</code> array (and will also create the correct amount of questionmarks for the unnamed placeholders). It will then loop through the array binding the <code>POST</code> values to the correct questionmark.</p>
  
  <p>This code works with no issues, but im now trying to adapt it for an <code>UPDATE</code> query rather than an <code>INSERT</code>, which is where im hitting a stumbling block:</p>
  
  <p>The <code>INSERT</code> query has a full list of database column titles and then a full list of their values, whereas the <code>UPDATE</code> needs them in title:value pairs (as far as I can see anyway!).</p>
  
  <p>Is there a way of updating using 2 separate lists (as per <code>INSERT</code>) or do I have to split the <code>$first</code> array and the <code>POST</code> array up into 'pairs'?</p>
  
  <p><em><strong>Solution</em></strong></p>
  
  <p>The following is the working SQL statement:</p>
  
  <pre><code>$sql = "UPDATE first_page_data SET " . implode(", ", array_map(function($v){ return "$v=?"; }, array_values($combined))) . " WHERE email = ?";
  </code></pre>
  
  <p>Its based heavily on the accepted answer, with a few tweaks to get it working!</p>