简体   繁体   English

具有相同名称的多个文本输入 - 添加到数据库

[英]Multiple text inputs with same name - add to database

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 我想使用foreach向数据库中的表添加多行,但每次尝试时都会出错

"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 当我打印出数据时,它将所有字段显示为数组,所以我必须对foreach语句做错了,但我不知道是什么

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的代码

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;
    }

Try this (assuming your form got same number of elements for each of the child_name, child_age etc): 试试这个(假设你的表单为每个child_name,child_age等获得相同数量的元素):

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.
}

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);

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM