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!
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>
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]
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.
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)
<?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);
?>
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]
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:
<?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.
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:
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: