将数组添加到现有数组并使用CodeIgniter进行保存

时间:2013-05-19 18:42:08

标签: php codeigniter codeigniter-2

我有以下功能:

function saveCOA() {
    $labref = $this->uri->segment(3);   
    $data = $this->input->post('compedia');
    $data1 = $this->input->post('specification');
    count($data) == count($data1); 
    for ($i = 0; $i < count($data); $i++) {
        $insert_data = array(
            'labref' => $labref, //NDQA201303001                
            'compedia' => $data[$i],
            'specification' => $data1[$i]
        );
        $this->db->insert('coa_body', $insert_data);
    }

它保存得很好,但现在我修改了它,如下所示:

$labref=$this->uri->segment(3);
$test_id=  $this->getRequestedTestIds($labref);
$data = $this->input->post('compedia');
$data1 = $this->input->post('specification');
count($data) == count($data1) && count($data)==  count($test_id); 
for ($i = 0; $i < count($data); $i++) {
    $insert_data = array(
        'labref' => $labref, //NDQA201303001 - Same for all the rows
        'test_id'=>$test_id[$i], //added array
        'compedia' => $data[$i],
        'specification' => $data1[$i]
    );
    $this->db->insert('coa_body', $insert_data);
}

$ test_id的值由print_r()打印为:

Array ( [0] => Array ( [test_id] => 5 ) [1] => Array ( [test_id] => 7 ) [2] => Array ( [test_id] => 9 ) [3] => Array ( [test_id] => 10 ) ) 

我添加了数组$test_id,然后返回:

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `coa_body` (`labref`, `test_id`, `compedia`, `specification`) VALUES ('NDQA201303001', Array, 'Alphy', 'poxy')

Filename: C:\xampp\htdocs\NQCL\system\database\DB_driver.php

Line Number: 330

我错过了什么?

1 个答案:

答案 0 :(得分:1)

看起来$test_id[$i]$test_id[0]是一个包含键test_id的数组,所以你给mysql查询一个数组。而是这样做:

'test_id' => $test_id[$i]['test_id'], //added array

EG。 print_r($test_id[0])会给你:

Array ( [test_id] => 5 )

我希望你明白。祝你有愉快的一天。