title: How can you update a few among many possible parameters in a PDO prepared statement
body: <p>For a prepared statement having say 5 named parameters, what if you pass only 3?</p>
  <pre><code>$sql = &quot;UPDATE students SET firstname = :firstname, lastname = :lastname, id = :id, major = :major, minor = :minor&quot;;
  
  $stmt= $dbh-&gt;prepare($sql);
  </code></pre>
  <p>I wanna update only the major and minor fields, what if I do <code>$stmt-&gt;execute([&quot;:major&quot; =&gt; &quot;biology&quot;, &quot;minor&quot; =&gt; &quot;physics&quot;]);</code>? Will the remaining (firstname, lastname, id) be untouched (original values) and only the provided params be touched?</p>
  <p>I have tried it and it throws</p>
  <blockquote>
  <p>Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens</p>
  </blockquote>
  <p>It seems that I must pass the exact number of params defined in a prepared stmt. What if I have many fields, and I have a <em>catch-all</em> update SQL statement but most of the times I just wanna update some of the params? Say there are 50 potential update parameters, writing one sql each is too repeatable and will be a maintenance nightmare.</p>
  <p>To reiterate, you can potential update a - z params but most of the times just a select few. How do you do it?</p>
  <p>I want this, but not possible</p>
  <pre><code>$sql = &quot;UPDATE alphabets SET a = :a, ..., z = :z&quot;;
  $stmt = $dbh-&gt;prepare($sql);
  $stmt-&gt;execute([&quot;:d&quot; =&gt; &quot;foo&quot;, &quot;:r&quot; =&gt; &quot;boo&quot;]);
  </code></pre>
  <p>Possible but don't want it</p>
  <pre><code>$sqlforA = &quot;UPDATE alphabets SET a = :a&quot;; //one for each updatable param
  ...
  $sqlForZ = &quot;UPDATE alphabets SET z = :z&quot;;
  
  $updateArr; //associative array containing each updating key/value
  foreach ($updateArr as $k =&gt; $v) {
    switch($k) {
    case &quot;a&quot;:
      $stmt = $dbh-&gt;prepare($sqlForA);
      $stmt-&gt;execute([&quot;:a&quot; =&gt; $v]);
      break;
    ...
    case &quot;z&quot;:
      $stmt = $dbh-&gt;prepare($sqlForZ);
      $stmt-&gt;execute(&quot;:z&quot; =&gt; $v);
      break;
    }
  }
  </code></pre>