How to insert the multi-select value in codeigniter?

Member

by aubrey , in category: PHP CMS , 9 months ago

How to insert the multi-select value in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 9 months ago

@aubrey 

To insert a multi-select value in CodeIgniter, you need to follow these steps:

  1. Create a form with a multi-select dropdown input field in your view file. Here is an example of a multi-select dropdown in HTML:
1
2
3
4
5
<select name="colors[]" multiple>
  <option value="red">Red</option>
  <option value="blue">Blue</option>
  <option value="green">Green</option>
</select>


  1. In your controller file, retrieve the selected values from the multi-select dropdown and format them as a comma-separated string before inserting them into the database. Here is an example:
1
2
3
4
5
6
$colors = implode(',', $this->input->post('colors'));
$data = array(
   'colors' => $colors
);

$this->db->insert('table_name', $data);


  1. Make sure to set your database table column as VARCHAR and large enough to store the selected values.
  2. Remember to validate and sanitize the input values before inserting them into the database to prevent SQL injection and other security vulnerabilities.


By following these steps, you should be able to successfully insert a multi-select value in CodeIgniter.