Page 1 of 1

Submit Multiple Records using PHP IMPOSSIBLE???

Posted: Fri Apr 06, 2007 10:41 am
by hdogg
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Submit Multiple Records using PHP IMPOSSIBLE???

Hi... I've been having a hard time trying to get php to submit multiple records.


First Off... This following form works perfectly when I try to insert multiple rows of one field type - forecast[].

However, I want to submit both - forecast[] and month[] form fields.



HTML Form 
[syntax="html"]
<input type=text name=forecast[] value=$forecast> <input type=text name=month[] value=$forecast><BR>

<input type=text name=forecast[] value=$forecast> <input type=text name=month[] value=$forecast><BR>

<input type=text name=forecast[] value=$forecast> <input type=text name=month[] value=$forecast><BR>

<input type=submit>
Process Form
(for one field type)

Code: Select all

<?php

foreach ($_POST['forecast'] as $row=>$name)
 {
   
	
	$monthperiod = "" . $day_oracle[$month] . "-" . $month_oracle[$month] . "-" . '0'. $year;
	$forecast = $name;
	$query_insert_batch = oci_parse($c, "INSERT INTO FORECAST_INPUT(FORECAST) VALUES ('$name')");
	
	oci_execute($query_insert_batch);
?>
-Hyrum


Everah | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Apr 06, 2007 11:13 am
by volka
try something like

Code: Select all

<input type="text" name="r[0][forecast]"><input type="text" name="r[0][month]" /><br />
<input type="text" name="r[1][forecast]"><input type="text" name="r[1][month]" /><br />
<input type="text" name="r[2][forecast]"><input type="text" name="r[2][month]" /><br />

Posted: Fri Apr 06, 2007 11:29 am
by hdogg
the problem that i am having is the php code...

Posted: Fri Apr 06, 2007 11:32 am
by RobertGonzalez
Month and forecast are separate arrays. You will either need to loop each individually or make sure they are the same size and then use the same index in a for loop to access each one's properties.

Posted: Fri Apr 06, 2007 11:41 am
by hdogg
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Is this getting closer??? And is my current field names forecast[] and month[] okay?

Process Form
(for one field type)

Code: Select all

<?php

foreach ($_POST as $row=>$name)
 {
   
///----- is this the proper way to identify the array
 $month [$row];
 $forecast [$row];

	
	
	$query_insert_batch = oci_parse($c, "INSERT INTO FORECAST_INPUT(FORECAST , MONTH) VALUES ('$forecast', '$month')");
	
	oci_execute($query_insert_batch);
?>
-Hyrum


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Apr 06, 2007 12:17 pm
by Mightywayne
When you submit a post, please put it in the [ php ] tags. ;) But I think what you want is...

[everah said it better]

Sure hope I helped someone for once.

Edit: Yeah I uh. I didn't. The forum lagged a bit. Weird.

Posted: Fri Apr 06, 2007 12:22 pm
by hdogg
what the heck? spam post?

Posted: Fri Apr 06, 2007 12:22 pm
by RobertGonzalez
Remember, post is an array. forecast and month are keys in the POST array which happen to be arrays themselves. So you would have to do something like:

Code: Select all

<?php
// Make sure the arrays aren't empty, as you will get nothing from them if they are
if (!empty($_POST['forecast']) && !empty($_POST['month']))
{
    $forecast = $_POST['forecast'];
    $forecast_count = count($forecast);
    $month = $_POST['month'];
    $month_count = count($month);

    // This checks to make sure the two arrays are the same size
    if ($forecast_count == $month_count)
    {
        for ($i =0; $i < $forecast_count; ++$i)
        {
            $query_insert_batch = oci_parse($c, "INSERT INTO FORECAST_INPUT(FORECAST , MONTH) VALUES ('{$forecast[$i]}', '{$month[$i]}')");
            oci_execute($query_insert_batch); // You should add an error handler here as well in case something borks
        }
    }
    // This would be a great place to handle differences in the array sizes of forecast and month
}
// This would be a great place to handle empty forecast and month arrays
?>
This is untested, but hopefully gives you some idea of a place to start.

Posted: Fri Apr 06, 2007 12:26 pm
by hdogg
Everah -

Thanks a bunch... i think that's going to work... i was getting hung up on the whole can and managing the two...

Have a wonderful day!

Cheers-

Hyrum

Posted: Fri Apr 06, 2007 12:46 pm
by RobertGonzalez
Just make sure you test it before tossing it into production.

Re: Submit Multiple Records using PHP IMPOSSIBLE???

Posted: Fri Apr 06, 2007 3:20 pm
by Christopher
I just wanted to follow up on volka's post to note that using the array syntax in form inputs without indexes is not really proper in this context. I believe the only place where "varname[]" is proper is for checkboxes and multiple-selects. So in this case it would be:

Code: Select all

<input type="text" name="forecast[0]" value=""> <input type="text" name="month[0]" value=""><br/>
<input type="text" name="forecast[1]" value=""> <input type="text" name="month[1]" value=""><br/>
<input type="text" name="forecast[2]" value=""> <input type="text" name="month[2]" value=""><br/>
I would also note that PHP's ability to convert request vars into arrays is not the only way to do this. It is language specific. You could also simply do:

Code: Select all

<input type="text" name="forecast0" value=""> <input type="text" name="month0" value=""><br/>
<input type="text" name="forecast1" value=""> <input type="text" name="month1" value=""><br/>
<input type="text" name="forecast2" value=""> <input type="text" name="month2" value=""><br/>
and then do something like this:

Code: Select all

for($i=0; $i<3; ++$i) {
     echo $_POST['forecast' . $i];
     echo $_POST['month' . $i];
}