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.
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:
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'];
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.
$insertAddress="INSERT INTO address (idperson, addressLineOne, addressLineTwo)
VALUES ( '$id','$_SESSION[addressLineOne]','$_SESSION[addressLineTwo]')";
if (!mysql_query($insertAddress,$con))
{die('Error: ' . mysql_error());}
echo "";
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:
foreach($_SESSION['idperson'] as $row=>$id)
{
$idperson=$id;
$addressLineOne=$_SESSION['addressLineOne'][$row];
$addressLineTwo=$_SESSION['addressLineTwo'][$row];
}
//enter rows into database
foreach($_SESSION['idperson'] as $row=>$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";
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.
Here is the code i'm using to add another table on address.php:
<?php for($i=1; $i<6; $i++):?>
<div id='hidden<?php echo $i; ?>' style='display: none'>
<tr class="odd">
<td width="33%">Street/Number</td>
<td><input type="text" name="addressLineOne[]" id="addressLineOne<?php echo $i; ?>"/></td>
</tr>
<tr>
<td>Address 2</td>
<td><input type="text" name="addressLineTwo[]" id="addressLineTwo<?php echo $i; ?>"/></td>
</tr>
<?php endfor; ?>
Submit.php doesn't have anything besides connecting to the db and the above insert.
session_register
is deprecated? and that its usage with$_SESSION
will lead to crazy, wild and, unpredictable results (according to the manual)? – Mark Elliot Jan 23 '11 at 4:16submit.php
or this "inserting array" thing that you're doing. – Mark Elliot Jan 23 '11 at 4:21