title: Foreach PHP only inserts last item in db
body: <p>i allready found some answers at other topics but not a working answer for my situation. 
  I'm having some troubles with inserting elements of an array into my DB. (SQL Server). When using a foreach loop in PHP the loop only inserts last element of the array into db, but when i <code>printr($arrayname)</code> it shows the correct both elements.</p>
  
  <p>Here's some of the code.</p>
  
  <p><strong><em>First i do a select to assign some variables:</em></strong> (the subquery is for removing ',' in the select)</p>
  
  <pre><code>$sql="SELECT ... FROM ...";
  $query = sqlsrv_query( $conn, $sql, array(), array( "Scrollable" =&gt; SQLSRV_CURSOR_KEYSET ));
  
  foreach($_SESSION['cart'] as $id =&gt; $value) { 
  
  $sql.=$id.","; 
   }         
  $sql=substr($sql, 0, -1).")"; 
  
       if ($query){
  
           while($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC) ) {
  
           $variable=$row['rowname'];
  
          }
      }
  </code></pre>
  
  <p>So far so good, did an echo on the <code>$variable</code> to test if it prints both row records, and it did.</p>
  
  <p><strong><em>Than i do the INSERT:</em></strong> </p>
  
  <pre><code>foreach($_SESSION['cart'] as $id =&gt; $value) { 
  
  print_r($id);
  
  $sql="INSERT INTO() ... VALUES ('$value')";
      $query= sqlsrv_query( $conn, $sql, array(), array( "Scrollable" =&gt; SQLSRV_CURSOR_KEYSET ));
  }
  </code></pre>
  
  <p>Here's where it goes wrong, again i did a <code>print($id)</code> to test and see the values. 
  It prints both values but only inserts the last element (<code>$variablename</code>) of array <code>$id</code>.</p>
  
  <p>Please, what am i doing wrong?</p>
  
  <p>Cheers.</p>