1

i am sending $_POST['checkbox_name'] to function insert_to_table.

function insert_to_table($valid_array)
    {
        $data_array = array();

        $this->load->model('get_data_model');
        $updated_max_brand_id = $this->get_data_model->get_max_brand_id();

        foreach ($valid_array as $key => $value) {
            $data_array['bdc_brand_id'] = $updated_max_brand_id;
            $data_array['bdc_cat_id'] = $value;
        }

        $this->db->insert('mart_brand_dealing_cat',$data_array);
    }

the final mysql query should run as below

INSERT INTO `mart_brand_dealing_cat` (`bdc_brand_id`, `bdc_cat_id`) VALUES (11,43),(11,42);

11 - updated_max_brand_id; 42,43 are coming from already existed array $valid_array.

I am trying to insert multiple values at a time.How can i do it. i may wrong please guide and help me.

2
  • $data_array['bdc_cat_id'] = $valid_array should perhaps be $data_array['bdc_cat_id'] = $value, what $valid_array look like? Commented Feb 24, 2015 at 19:14
  • you are right @LawrenceCherone edited wuestion. i was trying and just pasted same code here and forgot to change. Thank you. Commented Feb 24, 2015 at 19:17

1 Answer 1

1

Your probably looking for something like $this->db->insert_batch();

So for example:

<?php 
function insert_to_table($valid_array)
{
    $this->load->model('get_data_model');
    $brand_id = $this->get_data_model->get_max_brand_id();

    $insert = array();
    foreach ($valid_array as $key => $cat_id) {
        $insert[] = array(
            'bdc_brand_id' => $brand_id,
            'bdc_cat_id' => $cat_id,
        );
    }

    if (!empty($insert)) {
        return $this->db->insert_batch('mart_brand_dealing_cat', $insert); 
    } else {
        return false;
    }
}
?>
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.