在Codeigniter中将多个选择值插入数据库

在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.

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);
    }
}

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);
}

Do like this

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

Codeigniter Inserting Data

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')

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);
}

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.

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);