nikolas nikolas -4 years ago 192
PHP Question

Inserting Multiple Select Value to database in Codeigniter

I'm trying to input multiple select values in Database. Here is my demo code:

https://jsfiddle.net/wuarv946/

My controller:

public function create()
{
$data= array(
'name' => $this->input->post('name'),
'foods' => $this->input->post('foods')
);
$this->food_model->add_food($data);
}


My model:

public function add_food($data)
{
$this->db->insert('order_items', $data);
}


It only insert the last value of select. How to fix that.

Answer Source

Try to loop your foodlist.

My controller:

public function create()
{
    $food_list = $this->input->post('foods');
    foreach($food_list as $food) {
    $data= array(
        'name' => $this->input->post('name'),
        'foods' => $food
    );
    $this->db->insert('order_items', $data);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download