8
votes

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?
    }
}
2
So whats the error or problem? - Abhinav
so where is $data values?? - Abdulla Nilam
@IdentityUnkn0wn it only inserts one row, and $data['candidate_no'] = 0. - Choudhury Saadmaan Mahmid
In your if condition just echo $this->input->post('candidate_no'); and check whether you are getting any results? - Abhinav
@IdentityUnkn0wn No, I'm not getting anything. - Choudhury Saadmaan Mahmid

2 Answers

2
votes

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.

1
votes

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