0

I have array i want insert into the values in database using codeigniter, i don't know how to insert , i trying but i am not able to get the answer

My model

print_r($subjectHandling);


 Array
(
    [subjectId] => Array
        (
            [0] => 1
            [1] => 2
        )

)

now i want insert the values in database in this values.

I am trying like this

    foreach($subjectHandling as $key=>$value) {

    $reg_dat = array(
        'statffId'   => '1',
        'subjectId'      => $value,
    );
 $this->db->insert("subject_handling" , $reg_dat);

}

I m getting error ** Array to string conversion** , so how do this. i want to insert two roes in databse

1 Answer 1

1

This should work

$subjectHandling['subjectId'] = array(1, 2); 

$reg_dat = array();
foreach($subjectHandling['subjectId'] as $key => $value) {
 $reg_dat[] = array('staffId'=> 1, 'subjectId' => $value);
}
$this->db->insert_batch('subject_handling', $reg_dat);

https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.