title: Can you use parametrized PDO to add columns AND values?
body: <p>I have the following code which works, but it's not using <code>PDO</code> which is what I normally use:</p>
  
  <pre><code>&lt;?php
      if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  
          // will go all the way upto 50
          $fields = array('db_field1'=&gt;'cb1', 'dbfield2'=&gt;'cb2', 'dbfield3'=&gt;'cb3', 'dbfield4'=&gt;'cb4');
  
          $update = '';
  
          foreach($fields as $dbfield =&gt; $field) {
              if ($update) $update.= ',';
  
              $update.= ' '.$dbfield.'=';
  
              if (isset($_POST[$field])) {
                  $update.= 1;
              } else {
                  $update.= 0;
              }
          }
  
          // show generated query
          echo 'UDPATE table SET'.$update.' WHERE 1=1';
      }
  ?&gt;
  
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;form method="post"&gt;
              &lt;input type="checkbox" name="cb1" /&gt;
              &lt;input type="checkbox" name="cb2" /&gt;
              &lt;input type="checkbox" name="cb3" /&gt;
              &lt;input type="checkbox" name="cb4" /&gt;
              &lt;!-- all the way to 50 --&gt;
  
              &lt;input type="submit" value="submit" /&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Because I am getting the <code>column names</code> AND the <code>column values</code> to be updated from the code above, I am not sure how to do this using <code>PDO</code>?</p>
  
  <p>I have done the following code below:</p>
  
  <pre><code>&lt;?php
      if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  
          $fields = array('db_field1'=&gt;'cb1', 'dbfield2'=&gt;'cb2', 'dbfield3'=&gt;'cb3', 'dbfield4'=&gt;'cb4');
  
          $update = '';
  
          foreach($fields as $dbfield =&gt; $field) {
              if ($update) $update.= ',';
  
              $update.= ' '.$dbfield.'=';
  
              if (isset($_POST[$field])) {
                  $update.= 1;
              } else {
                  $update.= 0;
              }
          }
  
          $DBH = new PDO( "mysql:host=localhost;dbname=database", "user", "pass" );
          $DBH -&gt; setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  
          $STH = $DBH -&gt; prepare( "update table1 set :update where id = :id" );
  
          $STH -&gt; bindParam( ':update', $update, PDO::PARAM_STR, 255 );
          $STH -&gt; bindParam( ':id', $id, PDO::PARAM_INT, 4 );
  
          $STH -&gt; execute();
      }
  ?&gt;
  
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;form method="post"&gt;
              &lt;input type="checkbox" name="cb1" /&gt;
              &lt;input type="checkbox" name="cb2" /&gt;
              &lt;input type="checkbox" name="cb3" /&gt;
              &lt;input type="checkbox" name="cb4" /&gt;
              &lt;!-- all the way to 50 --&gt;
  
              &lt;input type="submit" value="submit" /&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>But it gives me the following error:</p>
  
  <blockquote>
    <p>[Tue Jul 19 09:15:44 2011] [error] [client ::1] PHP Fatal error: 
    Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:
    Syntax error or access violation: 1064 You have an error in your SQL
    syntax; check the manual that corresponds to your MySQL server version
    for the right syntax to use near '' db_field1=0, dbfield2=1,
    dbfield3=1, dbfield4=0' where id = 1' at line 1' in
    /var/www/page1.php:30\nStack trace:\n#0
    /var/www/page1.php(30): PDOStatement->execute()\n#1
    {main}\n  thrown in /var/www/page1.php on line 30,
    referer: http:// localhost/page1.php</p>
  </blockquote>