title: PDO insert array values
body: <p>I have a form which allows me to insert a domain to a 'domains' table in my database. </p>
  
  <p>Part of the form includes a list of services that I provide for that domain which are presented as a series of checkboxes and handled as an array. These services are inserted into a marketing_lookup table that has two columns the has the domain id and the service id.</p>
  
  <p>I'm trying to rewrite mysql insert statements using PDO.</p>
  
  <p>I can code inserting the domain to the domains table.</p>
  
  <p>I need help inserting the services array into the  marketing_lookup table.
  The services </p>
  
  <p>The html form on my page</p>
  
  <pre><code>&lt;form ....&gt;
  ...
  &lt;input type='checkbox' name='services[]' value='1'&gt;Service 1&lt;br&gt;
  &lt;input type='checkbox' name='services[]' value='2'&gt;Service 2&lt;br&gt;
  ...
  &lt;/form&gt;
  </code></pre>
  
  <p>I have copied and pasted and edited this so far</p>
  
  <pre><code>... 
  code inserting the domain into the domain table here
  ...
  //start inserting services here
  if ($services == '') $services = array();
  $services = $_POST['services']; 
  $id = $conn-&gt;lastInsertId(); //obtained from above
  
  if (!isset($_POST['services'])):
  echo  'Nothing Selected';
  
  else:
  try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $stmt = $conn-&gt;prepare('INSERT IGNORE INTO marketing_lookup SET   
  `domain_id` = :id,
  `service_id` = :serviceid')
  
  foreach ($services as $serviceid) {
  $a = array  (':1'=&gt;$serviceid['1'],
               ':2'=&gt;$serviceid['2']);
  
   if ($stmt-&gt;execute($a)) {          
         //Query succeeded
              }
      else {
                  // Query failed.
                  echo $q-&gt;errorCode();
                  }    
  // close the database connection
  $conn = null;   
  } // end foreach
    } //end try
  
  catch(PDOException $e) {
          echo $e-&gt;getMessage();
          }
  endif;  
  ?&gt;
  </code></pre>