title: How to Insert Session Array into MySQL DB using PHP
body: <p>I have a multi page form and am trying to capture an array as a session to insert into mysql. Page two of the form is the  "Address" section. Here I ask for Address information. The user can hit a "enter another address" button which expands another box.</p>
  
  <p>The user hits next to go to the next page (phone.php) where I ask for phone info. On this page I think I am  registering the session data correctly like so:</p>
  
  <pre><code>if(!isset($_SESSION)) 
  { 
  @session_start(); 
  }  
  header("Cache-control: private");
  ob_start();
  
  include('header.php');
  
  //now, let's register our session variables from address.php
  session_register('addressLineOne');
  session_register('addressLineTwo');
  
  //finally, let's store our posted values in the session variables
  $_SESSION['addressLineOne'] = $_POST['addressLineOne'];
  $_SESSION['addressLineTwo'] = $_POST['addressLineTwo'];
  </code></pre>
  
  <p>Eventually we hit the submit.php . If I disable the code to add an additional address, this is the insert I am using which works fine.</p>
  
  <pre><code> $insertAddress="INSERT INTO address (idperson, addressLineOne, addressLineTwo)
   VALUES ( '$id','$_SESSION[addressLineOne]','$_SESSION[addressLineTwo]')";
   if (!mysql_query($insertAddress,$con))
     {die('Error: ' . mysql_error());}
   echo "";
  </code></pre>
  
  <p>When I enable the "add additional address" code, the insert above will insert "ARRAY" into every DB field. I have been trying out the following:</p>
  
  <pre><code> foreach($_SESSION['idperson'] as $row=&gt;$id)
  {
      $idperson=$id;
      $addressLineOne=$_SESSION['addressLineOne'][$row];
      $addressLineTwo=$_SESSION['addressLineTwo'][$row];
  }
   //enter rows into database
   foreach($_SESSION['idperson'] as $row=&gt;$id)
  {
      $idperson=mysql_real_escape_string($id);
      $addressLineOne=mysql_real_escape_string($_SESSION['addressLineOne'][$row]);
      $addressLineTwo=mysql_real_escape_string($_SESSION['addressLineTwo'][$row]);
  }
  
   $insertAddress="INSERT INTO address (idperson, addressLineOne, addressLineTwo)
   VALUES ('.$id.','.$addressLineOne.','.$addressLineTwo.')";
  
   if (!mysql_query($insertAddress,$con))
   {
   die('Error: ' . mysql_error());
   }
   echo "$row record added";
  </code></pre>
  
  <p>Instead of working, however, the above just inserts a couple of dots like "..." into the database. I don't know what else to do. Am I not grabbing the session data correctly? Is my foreach code way off? Thanks for any help.</p>
  
  <p>Here is the code i'm using to add another table on address.php:</p>
  
  <pre><code> &lt;?php for($i=1; $i&lt;6; $i++):?&gt;
   &lt;div id='hidden&lt;?php echo $i; ?&gt;' style='display: none'&gt;
  &lt;tr class="odd"&gt;
      &lt;td width="33%"&gt;Street/Number&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="addressLineOne[]" id="addressLineOne&lt;?php echo $i; ?&gt;"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;Address 2&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="addressLineTwo[]" id="addressLineTwo&lt;?php echo $i; ?&gt;"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
   &lt;?php endfor; ?&gt; 
  </code></pre>
  
  <p>Submit.php doesn't have anything besides connecting to the db and the above insert.</p>