Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following function:

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

It saves well, but now I have modified it as shown below:

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

The value of $test_id is printed out by print_r() as:

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

I have added the array $test_id, and it returns:

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

What am I missing?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Looks like $test_id[$i] or $test_id[0] is an array which contains key test_id so you're giving a mysql query an array. Instead do this:

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

Eg. print_r($test_id[0]) would give you:

Array ( [test_id] => 5 )

I hope you get it. Have a nice day.

share|improve this answer
    
Thanks, that works great –  alphy May 19 '13 at 19:10
    
You're welcome. Good luck with your project. –  Daniel May 19 '13 at 19:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.