title: IMSSP "Tried to bind parameter number 0" error when binding array values in foreach loop
body: <p>I have a PHP associative array, the values of which I want to bind with PDO in a foreach loop. So far I have this:</p>
  
  <pre><code>$sqlSecondaryInsert = "INSERT INTO TCMS_Documents ";
  
  // Table Fields
  $sqlSecondaryFields = " (DocumentID, ";
  $sqlSecondaryValues = "VALUES (:lastDocumentInsertID, ";
  
  foreach ($intersectArray as $key =&gt; $value) {
      if (trim($value) != '') {
          $sqlSecondaryFields.= trim($key) . ", ";
          $sqlSecondaryValues.= ":" . trim($key) . ", ";
      }
  }
  
  $sqlSecondaryFields = rtrim($sqlSecondaryFields, ", ");
  $sqlSecondaryFields.= ") ";
  
  $sqlSecondaryValues = rtrim($sqlSecondaryValues, ", ");
  $sqlSecondaryValues.= ")";
  
  $sqlSecondaryStmt = $sqlSecondaryInsert . $sqlSecondaryFields;
  
  $stmt2 = $connPDO-&gt;prepare($sqlSecondaryStmt);
  
  $stmt2-&gt;bindValue(':lastDocumentInsertID', $lastDocumentInsertID);
  
  foreach ($intersectArray as $key =&gt; $value) {
      error_log("attempting to bind " . $key . " to value " . $value);
      $stmt2-&gt;bindValue(':' . $key, $value, PDO::PARAM_STR);
  }
  
  $stmt2-&gt;execute();
  </code></pre>
  
  <p>While the SQL INSERT statement is being generated correctly, I receive the following in my PHP error log when trying to execute it: </p>
  
  <pre><code>"IMSSP",-29,"Tried to bind parameter number 0.  SQL Server supports a maximum of 2100 parameters."
  </code></pre>
  
  <p>The PHP log is showing the correct keys and values from the error_log within the foreach loop, so I don't understand why the PDO bindValues are not working.</p>
  
  <p>An example <code>print_r</code> of <code>$intersectArray</code> can be seen here: </p>
  
  <pre><code>[DocumentNumber] =&gt; 123
  [ValidFromDate] =&gt; 02/09/2015
  [ValidExpiryDate] =&gt; 26/09/2015
  </code></pre>
  
  <p><strong>Edit</strong>: Some additional information that may or may not be useful: we are using SQL Server 2008. I don't know if this is relevant, I have no understanding of how the PDO drivers differ for SQLSRV and MySQL...</p>