2013-05-19 82 views
0

保存我有以下功能:添加一個陣列到現有陣列,並用笨

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

我已經添加了陣列它返回:

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

看起來像$test_id[$i]$test_id[0]是一個數組,其中包含密鑰test_id所以你給mysql查詢一個數組。而是這樣做:

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

例如, print_r($test_id[0])會給你:

Array ([test_id] => 5) 

我希望你得到它。祝你今天愉快。

+0

謝謝,這很好用 – alphy

+0

不客氣。祝你的項目好運。 –