3

I have a form with several fields, all of which can be multiplied

<input type="text" name="child_name[]" />
<input type="text" name="child_age[]" />
<input type="text" name="child_gender[]" />    
<input type="text" name="child_school[]" />

I want to add multiple rows to a table in the database using a foreach, but every time I try I get an error saying

"Unknown column 'Array' in 'field list'"

When i print out the data it shows all of the fields as arrays, so I must be doing something wrong with the foreach statement, but I have no idea what

Array ( [child_name] => Array ( [0] => child one [1] => child two) [child_age] => Array ( [0] => 14 [1] => 13 ) [child_gender] => Array ( [0] => male [1] => female ) [child_school] => Array ( [0] => burnside [1] => summer heights high ) )

Any help would be greatly appreciated!#

UPDATED

Here is the code for my foreach

foreach ($_POST['child_name'] as $child_name)
    {
        $insert_children_data = array(
            'child_name' => $_POST['child_name'],
            'child_age' => $_POST['child_age'],
            'child_gender' => $_POST['child_gender'],
            'child_school' => $_POST['child_school']
        );
        $insert = $this->db->insert('portrait_children', $insert_children_data);
        return $insert;
    }
3
  • 2
    Show your query please. Commented Apr 9, 2013 at 9:23
  • what code you have in foreach..? Commented Apr 9, 2013 at 9:24
  • you must use two foreach, one for $_POST fields, second for fields in key. Commented Apr 9, 2013 at 9:27

3 Answers 3

4

Try this (assuming your form got same number of elements for each of the child_name, child_age etc):

for ($ix=0; $ix<count($_POST['child_name']); $ix++)
{
    $insert_children_data = array(
        'child_name' => $_POST['child_name'][$ix],
        'child_age' => $_POST['child_age'][$ix],
        'child_gender' => $_POST['child_gender'][$ix],
        'child_school' => $_POST['child_school'][$ix]
    );

    $insert = $this->db->insert('portrait_children', $insert_children_data);
    //return $insert; //you cant return here. must let the loop complete.
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am receiving a syntax error "syntax error, unexpected ';'" at $ix=0; any ideas?
did you try the updated version. See the updated version above. I left a small typo and fixed it now.
YES it works! Thank you so very very much :) when I get a high enough reputation I will vote you up :)
0

Make one array. array contain all the data of particular child like

Array (
[0] => Array (
  [name] => child1 
  [age] => 5 
  [gender] => male
  [school] => 1school )
[1] => Array (
  [name] => child2 
  [age] => 10 
  [gender] => male
  [school] => school2 )

Please try below Code

$i =0;  

foreach($_REQUEST['child_name'] as $child)

{

$child1[$i]['name'] = $child;

$i++;

}

$i =0;  

foreach($_REQUEST['child_age'] as $child)

{

$child1[$i]['age'] = $child;

$i++;

}

$i =0;  

foreach($_REQUEST['child_gender'] as $child)

{

$child1[$i]['gender'] = $child;

$i++;

}

$i =0;  

foreach($_REQUEST['child_school'] as $child)

{

$child1[$i]['school'] = $child;

$i++;

} 
$insert = $this->db->insert('portrait_children', $child1);
    return $insert;

Comments

0

You are assigning an array to a key, which is not feasible, try looping all the elements

foreach ($_POST as $key)<br>
{<br>
    foreach($key as $v=>$v1)<br>
    {<br>
        $s[$v] = $v1;<br>
    }<br>

}<br>
print_r($s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.