8

I have a CodeIgniter application and a MySQL table with the following structure:

Table shortlisted_candidates
id int PRIMARY KEY AUTO INCREMENT,
candidate_no int NOT NULL,
written_marks int,
viva_marks int

I want to do insert_batch into this table, but data will only be inserted in id and candidate_no columns.

I know Codeigniter Active Records Class provides the $this->db->insert_batch() function for batch insert but it actually inserts data in the entire table, whereas I want data to be inserted only into specific columns. How can I achieve this in CodeIgniter?

Note that id is an AUTO INCREMENT, PRIMARY KEY column.

My Controller code:

class Shortlisted_candidates extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('Shortlisted_candidate');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->helper('form');
        $this->output->enable_profiler(false);
    }
    function add(){
        $data = array();
        if ($_POST) {

            $data['candidate_no'] = $this->input->post('candidate_no');
            $this->Shortlisted_candidate->add_candidate_to_shortlist($data);
            $this->session->set_flashdata('message', 'Data Successfully Added');
            redirect('shortlisted_candidates/add');
        }
    }
}

My Model code:

class Shortlisted_candidate extends CI_Model
{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function add_candidate_to_shortlist($data){
        //Following code inserts data in ALL COLUMNS
        $this->db->insert_batch('shortlisted_candidates', $data);
        //How to write active record batch insert query for inserting data in only `id` and `candidate_no` column?
    }
}
6
  • So whats the error or problem? Commented Jul 14, 2015 at 5:43
  • so where is $data values?? Commented Jul 14, 2015 at 5:43
  • @IdentityUnkn0wn it only inserts one row, and $data['candidate_no'] = 0. Commented Jul 14, 2015 at 5:53
  • In your if condition just echo $this->input->post('candidate_no'); and check whether you are getting any results? Commented Jul 14, 2015 at 6:05
  • @IdentityUnkn0wn No, I'm not getting anything. Commented Jul 14, 2015 at 6:15

2 Answers 2

2

you need to modify following controller function.

function add(){
    $data = array();
    if ($_POST) {

        $data[0]['candidate_no'] = $this->input->post('candidate_no');
        $this->Shortlisted_candidate->add_candidate_to_shortlist($data);
        $this->session->set_flashdata('message', 'Data Successfully Added');
        redirect('shortlisted_candidates/add');
    }
  }

apart from this you need to modify your table schema such that either set default value of other field.

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

Comments

1

You can define your column where you want to insert batch. So you have to make your array looks like

$data = array(
    array(
            'id' => $id,
            'candidate_no' => $candidate_no
    ),
    array(
            'id' => $id,
            'candidate_no' => $candidate_no
    )
);

//now in controller

 $this->Shortlisted_candidate->add_candidate_to_shortlist($data);

This will insert only specific column, not to entire table's column

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.