多个文本输入,相同的名字 - 添加到数据库 [英] Multiple text inputs with same name - add to database

查看:111
本文介绍了多个文本输入,相同的名字 - 添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格有几个领域,所有这一切都可以成倍

 <输入类型=文本名称=CHILD_NAME []/>
<输入类型=文本名称=child_age []/>
<输入类型=文本名称=child_gender []/>
<输入类型=文本名称=child_school []/>

我要在数据库中使用foreach多行添加到一个表,但每次我试图我得到一个错误说

 ,在字段列表未知列'阵'

当我打印出它显示的所有字段作为阵列的数据,所以我必须做一些错误的foreach语句,但我不知道是什么

 阵列([CHILD_NAME] =>数组([0] =>儿童[1] =>儿童二)[child_age] =>数组([0] => 14 [1] => 13)[child_gender] =>数组([0] =>男[1] =>女)child_school] =>数组([0] =>伯恩赛德[1] =>夏天高度高))

任何帮助将大大AP preciated#!

更新时间:

下面是code为我的foreach

 的foreach($ _ POST ['CHILD_NAME']为$ CHILD_NAME)
    {
        $ insert_children_data =阵列(
            CHILD_NAME'=> $ _ POST ['CHILD_NAME'],
            child_age'=> $ _ POST ['child_age'],
            child_gender'=> $ _ POST ['child_gender'],
            child_school'=> $ _ POST ['child_school']
        );
        $插入= $这个 - > DB-GT&;插入('portrait_children',$ insert_children_data);
        返回$插入;
    }


解决方案

试试这个(假设你的形式得到了每个CHILD_NAME的相同数量的元素,child_age等):

 为($九= 0; $九<计数($ _ POST ['CHILD_NAME']); $九++)
{
    $ insert_children_data =阵列(
        CHILD_NAME'=> $ _ POST ['CHILD_NAME'] [$九]
        child_age'=> $ _ POST ['child_age'] [$九]
        child_gender'=> $ _ POST ['child_gender'] [$九]
        child_school'=> $ _ POST ['child_school'] [$ IX]
    );    $插入= $这个 - > DB-GT&;插入('portrait_children',$ insert_children_data);
    //返回$插入; //你不能回到这里。一定要让循环完成。
}

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

解决方案

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

这篇关于多个文本输入,相同的名字 - 添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆