M5533 M5533 - 4 years ago 132
MySQL Question

How can i store multiple select box value in database in codeigniter?

I have one requirements form in my page..in that form i have users dropdown with select box. I want to select multiple users at a time and need to store selected users in database.


public function requirement()
$data['user'] = $this->LoginModel->getusers();

$insert = array (
'role_name' => $this->input->post('role_name'),
'vacancies' => $this->input->post('vacancies'),
'experience' => $this->input->post('experience'),
'jd' => $this->input->post('jd'),
'hiring_contact_name' => $this->input->post('hiring_contact_name'),
'hiring_contact_number' => $this->input->post('hiring_contact_number'),
'user_id' => $this->input->post('user_id')//this is my foreign key id from users table);


function getusers()
$query = $this->db->get();
//echo $this->db->last_query();
return $query->result();

View page

<div class="form-group">
<label>Choose Vendor</label>
<select id="chkveg"class="form-control" multiple class="form-control" data-placeholder="user name" name="user_id[]" >
<option value="0"></option>
foreach ($user as $rows)
<option value="<?php echo $rows->user_id ?>">
<?php echo ucfirst($rows->first_name) ?>

Answer Source

make select-box array instead of single value.

<select id="chkveg""  class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >

than you need to ALTER your column datatype to varchar or text.

after that you can implode function to create string value like

'user_id'=> implode(',',$this->input->post('user_id')); //eg.1,2,3

Reference link : http://www.w3schools.com/php/func_string_implode.asp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download