Submit Multiple Records using PHP IMPOSSIBLE???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hdogg
Forum Newbie
Posts: 13
Joined: Fri Apr 06, 2007 10:39 am

Submit Multiple Records using PHP IMPOSSIBLE???

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 />
hdogg
Forum Newbie
Posts: 13
Joined: Fri Apr 06, 2007 10:39 am

Post by hdogg »

the problem that i am having is the php code...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
hdogg
Forum Newbie
Posts: 13
Joined: Fri Apr 06, 2007 10:39 am

Post 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]
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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.
hdogg
Forum Newbie
Posts: 13
Joined: Fri Apr 06, 2007 10:39 am

Post by hdogg »

what the heck? spam post?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
hdogg
Forum Newbie
Posts: 13
Joined: Fri Apr 06, 2007 10:39 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just make sure you test it before tossing it into production.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Submit Multiple Records using PHP IMPOSSIBLE???

Post 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];
}
(#10850)
Post Reply