...

View Full Version : The $_POST array and mysql insert problem



MarioPro
09-08-2005, 09:03 PM
Hi,
First, thanks in advance for any help.

I'm trying to build a form where the form fields might repeat according with the user's selection. Like inserting multiple entries of a calendar. Here's a simple example:

user input: from 1 to 10 fields at a time // Then the page loads thhe form for (for example) 2 entries of
<input type="text" name="title[]">
<input type="text" name="location[]">
<input type="text" name="date[]">

Now, what I get from print_r($_POST); is the following:

Array (
[title] => Array ( [0] => The first one [1] => This is the second )
[location] => Array ( [0] => USA [1] => UK )
[date] => Array ( [0] => 2005/09/02 [1] => 2006/02/15 )
)

##--> Here's my difficulty:
I want to INSERT into the database but the array's are killing me as I'm unable to put the values correctly :(

devinemke
09-08-2005, 09:18 PM
a simple example:


<?php
if (!isset($_POST['submit']))
{
echo '<form action="" method="POST">';

for ($i = 1; $i <= 3; $i++)
{
echo $i . '<br>
title: <input type="text" name="data[' . $i . '][title]"><br>
location: <input type="text" name="data[' . $i . '][location]"><br>
date: <input type="text" name="data[' . $i . '][date]"><br><br>
';
}

echo '
<input type="submit" name="submit" value="submit">
</form>
';
}
else
{
foreach ($_POST['data'] as $value)
{
echo "INSERT INTO table SET title = '" . $value['title'] . "', location = '" . $value['location'] . "', date = '" . $value['date'] . "'<br>";
}
}
?>

MarioPro
09-08-2005, 09:26 PM
Thanks for your reply.

But isn't there a way of parsing the array I posted?


Array (
[title] => Array ( [0] => The first one [1] => This is the second )
[location] => Array ( [0] => USA [1] => UK )
[date] => Array ( [0] => 2005/09/02 [1] => 2006/02/15 )
)

devinemke
09-08-2005, 09:48 PM
But isn't there a way of parsing the array I posted?
yes, but that is not the most logical way of grouping the data given the way that you will ultimately do the queries.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum