1

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.

2
  • Can you post var_dump($data)? Commented Jan 6, 2016 at 7:45
  • Have you specified 'multiple' attribute in select eg: <select multiple> </select> Commented Jan 6, 2016 at 7:52

7 Answers 7

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@russell see my update. try to do that loop. if it doesn't work let me know what does var_dump($this->input->post('foods');
2

Try this.. Change select dropdown name with name="foods[]" .

<select name="foods[]" class="selectpicker" multiple title="Choose Foods" multiple data-max-options="2" data-live-search="true">
  <option value="1">Mustard</option>
  <option value="2">Ketchup</option>
  <option value="3">Relish</option>
</select>

Controller

public function create()
{
    $data= array(
        'name' => $this->input->post('name'),
        'foods' => implode(",",$this->input->post('foods')) // Store foods with comma separate 
    );
    $this->food_model->add_food($data);
}

Comments

1

change this in select name="foods[]"

try controller like this

public function create()
{
    $foods = $this->input->post('foods');
    $name= $this->input->post('name');
    $data = array();
    foreach( $foods as $k => $v){
      $data[$k]['name']=$name;
      $data[$k]['foods']=$v;
    }

    $this->food_model->add_food($data);
}

Comments

1

There are two mistakes in your form html code.

1) You have not specified form post method so its taking by default "get" method and you are trying to get values using post().

2) You have set the select multiple but did not make its name as array foods[].

Do like below:

<form method="post">
<div class="col-md-6">
<input type="name" placeholder="Enter name" class="form-control">
<select name="foods[]" class="selectpicker" multiple title="Choose Foods" multiple data-max-options="2" data-live-search="true">
  <option value="1">Mustard</option>
  <option value="2">Ketchup</option>
  <option value="3">Relish</option>
</select>
</div>
<button class="btn btn-primary">Submit</button>
</form>

And change controller as below:

public function create()
{
    $foods = $this->input->post('foods');
    $name= $this->input->post('name');
    $data = array();
    foreach( $foods as $key => $value){
      $data[$key]['name']=$name;
      $data[$key]['foods']=$value;
    }

    $this->food_model->add_food($data);

Hope this will clear you and solve your issue.

Comments

0

Do like this

$this->db->insert_batch('order_items', $data); 

Codeigniter Inserting Data

Comments

0

You need to batch insert e.g

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

Comments

0

Inserting without cycle is always a great idea. The right way is to using array of associative arrays and insert them using insert_batch Active Record function.

Here is the example of that:

$data = array(
   array(
      'title' => 'Title Text' ,
      'name' => 'Name Text' ,
      'date' => 'Date'
   ),
   array(
      'title' => 'Title Text 1' ,
      'name' => 'Name Text 1' ,
      'date' => 'Date 1'
   )
);

$this->db->insert_batch('mytable', $data); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.