在数据库的新行中插入几个值不同的数组


Insert several array of value different in new row from database

我想从数据库中插入行,数组的值,因为每个值都从数据库中插入新的行。

我不想使用序列化(json_encode或等)从数据库插入一行中的所有值。

例如:(在这个例子中,我想要三次插入数据(值),因为我有3个不同的值)更新4:

this my value:

<input name="name[0][]" value="11">
<input name="passport_number[0][]" value="11">

<input name="name[1][]" value="22">
<input name="passport_number[1][]" value="22">

我这样做了,但是它不工作:

$name_input = $this->input->post('name');
$passport_number_input        = $this->input->post('passport_number');
//$term_passport_input = $this->input->post('term_passport');
//$date_of_birth_input       = $this->input->post('date_of_birth');
//$age_input        = $this->input->post('age');
//$national_number_input = $this->input->post('national_number');
//$mobile_input       = $this->input->post('mobile');
//$address_input        = $this->input->post('address');
$data = array();
foreach ($name_input as $idx => $name) {
    $data[] = array(
        'name' => $name_input[0]
        'passport_number' => $passport_number_input[0],
        //'term_passport' => $term_passport_input[$idx],
        //'date_of_birth' => $date_of_birth_input[$idx],
        //'age' => $age_input[$idx],
        //'national_number' => $national_number_input[$idx],
        //'mobile' => $mobile_input[$idx],
        //'address' => $address_input[$idx],
    );
};
var_dump($name_input);
$this->db->insert_batch('customer_info', $data);

这是var_dump:

的输出'$name_input'
array(2) {
    [0] = > array(1) {
        [0] = > string(2)"11"
    }[1] = > array(1) {
        [0] = > string(2)"22"
    }
}

我得到这个错误:

遇到PHP错误
严重性:警告
消息:不能修改报头信息-报头已经发送(输出开始于)D: ' xampp '根'程序'控制器' admin ' tour_foreign.php: 405)
文件名:core/Common.php
行号:413

A数据库错误发生
错误编号:1054
字段中的未知列'0'INSERT INTO customer_info (0) VALUES ('22')
文件名:D:'xampp'htdocs'system'database'DB_driver.php
Line数量:330

Update 3: Per updated question (Update 4)
查看$name_inputvar_dump,下面的代码应该可以工作:

$name_input = $this->input->post('name');
$passport_number_input = $this->input->post('passport_number');
$data = array();
foreach ($name_input as $idx => $name) {
    $data[] = array(
        'name' => $name_input[$idx][0],
        'passport_number' => $passport_number_input[$idx][0]
    );
};
$this->db->insert_batch('customer_info', $data);

当POST输入作为array of arrays

传递时,需要进一步访问[0]元素。更新2:

看到这个问题是因为POST将数据作为一个进一步的数组返回,下面的代码可能会工作。我需要POST输入的var_dump ($hi_input ..)来给出确切的代码。

$hi_input    = $this->input->post('hi');
$hello_input = $this->input->post('hello');
$how_input   = $this->input->post('how');
$data = array();
foreach ($hi_input as $idx => $name) {
    $data[] = array(
        'hi' => $hi_input[$idx][0],
        'hello' => $hello_input[$idx][0],
        'how' => $how_input[$idx][0]
    );
};
$this->db->insert_batch('table', $data);

下面的代码应该工作,因为我已经测试了它(我没有安装CodeIgniter)。它不使用CodeIgniter来获取post数据。

$hi_input    = $_POST['hi'];
$hello_input = $_POST['hello'];
$how_input   = $_POST['how'];
$data = array();
foreach ($hi_input as $idx => $name) {
    $data[] = array(
        'hi' => $hi_input[$idx][0],
        'hello' => $hello_input[$idx][0],
        'how' => $how_input[$idx][0]
    );
}; 
$this->db->insert_batch('table', $data);

更新:
您也可以使用$this->db->insert_batch();来完成此操作,如下所示:

$hi_input    = $this->input->post('hi');
$hello_input = $this->input->post('hello');
$how_input   = $this->input->post('how');
$data = array();
foreach ($hi_input as $idx => $name) {
    $data[] = array(
        'hi' => $hi_input[$idx],
        'hello' => $hello_input[$idx],
        'how' => $how_input[$idx]
    );
};
$this->db->insert_batch('table', $data);
老回答


关于insert - http://codeigniter.com/user_guide/database/active_record.html#insert的CodeIgniter文档
声明$data参数是一个关联数组,以列名作为键,以数据作为值插入。

所以需要对每行调用一次。因此
试试这个:

$hi_input    = $this->input->post('hi');
$hello_input = $this->input->post('hello');
$how_input   = $this->input->post('how');
foreach ($hi_input as $idx => $name) {
    $data = array(
        'hi' => $hi_input[$idx],
        'hello' => $hello_input[$idx],
        'how' => $how_input[$idx]
    );
    $this->db->insert('table', $data);
};