sammy001 sammy001 - 3 months ago 9
MySQL Question

Not able to save checkbox values in database using codeigniter

I have a list of emails with checkbox, that look like this

enter image description here

I want that user is able to select multiple values and then save it in database

View

<?php
$reg_attributes = array('id'=>'form','class'=>'form-horizontal');
echo form_open('admin/assignrec/'.$data->id, $reg_attributes);
foreach($client as $rec_id): ?>
<tr>
<td> <?php echo form_checkbox('recid[]', $rec_id->id); ?> </td>
<td><?php echo $rec_id->email; ?></td>
</tr>
<?php
endforeach;
echo form_close();
?>


Controller

public function assignrec($dataid)
{
$chk = $this->input->post('recid');
$this->admin_model->assign_rec($quickdataid) ;
}


Model

public function assign_rec($dataid)
{
foreach ($chk as $chkid)
{
$data = array(
'requestid' => $dataid,
'recruiterid' => $this->input->post('chkid')
);
}
$insert_request = $this->db->insert('rec', $data);
return $insert_data;

}


However i am not able to save values in database, can anyone please help me

Answer

Your controller should be like

public function assignrec($dataid)
    {
        $quickdataid= $this->input->post('recid');
        $this->admin_model->assign_rec(dataid,$quickdataid) ; 
    }

Your model should be like

public function assign_rec($dataid,$chk)
    {
        foreach ($chk as $chkid)
            {
                $data = array(
                'requestid' => $dataid,
                'recruiterid' => $this->input->post('chkid')                        
                 );
            }
        $insert_request = $this->db->insert('rec', $data);
        return $insert_data;

    }