如何通过 codeigniter (view->controller->model) 在 mysql 中插入多选下拉值

How to insert multiselect dropdown values in mysql through codeigniter (view->controller->model)

View.php :- 使用 jquery 多选

<form class="form-horizontal" id="user_project" action="" method="POST" enctype="multipart/form-data">
<div class="row">
 <label class=" col-sm-3 col-form-label">Select Project Members :-</label>
 <div class="col-sm-8 form-group bmd-form-group">
  <select class="form-control" name="project_members[]" multiple 
     id="project_members">
   <?php 
    foreach($members as $mem){?>
    <option value="<?php echo $mem->email;?>"><?php echo $mem->firstname." 
     ".$mem->lastname;?></option>
   <?php }?>
  </select>
 </div>
 <label class="col-sm-1 label-on-right">
  <code>*</code>
 </label>
</div>

<button type="button" class="btn btn-rose" onclick="return 
  validate_Project();"> Add Project<div class="ripple-container"> </div> 
</button>
</form>

Scripting.js :- 用于验证和 ajax 调用

function validate_Project()
{
var level = document.getElementById("project_privacy").value;
var title = document.getElementById("project_title").value;
var domain = document.getElementById("project_domain").value;
var description = document.getElementById("project_description").value;
var guide = document.getElementById("project_guide").value;
var mem = document.getElementById("project_members").value;
var status = document.getElementById("project_status").value;
var category = document.getElementById("project_category").value;


var url;
if(save_method == 'update')
{
    url = "http://localhost/pccoespii/index.php/ricontroller/edit_project/";
}
else
{
    url = "http://localhost/pccoespii/index.php/ricontroller/float_project/";
}
//pass data to database using AJAX
$.ajax({
    url : url,
    type: "POST",
    data: $('#user_project').serialize(),
    dataType: "JSON",
    success: function(data)
    {
        //if success close modal and reload ajax table
        demo.showSwal('success-message');
    },
    error: function (xhr, ajaxOptions, thrownError)
    {
        alert(thrownError);
        console.warn(xhr.responseText);
    }
});

}

Controller.php

这是我想从视图中访问多选值的地方,这样我就可以将这些值推向模型,从而推向 MySql 数据库。 我也无法追踪控制器正在渲染的输出。 是否需要通过循环将值推送到数据库中? 另外请帮助我如何将这些数据发送到模型

public function float_project()
{
    $members = $this->input->post('project_members');
    foreach($members as $row)
    {
        $data= array(
            'project_members' => $row
        );
        echo(string($data));
    }
}

您的控制器无法读取 post 数据,因为您的表单没有用其方法属性值为 post 的表单标签包装。尝试用这个包裹你的表格。

<div class="col-sm-8 form-group bmd-form-group">
   <form action="" method="post"> 
       <select class="form-control" name="project_members[]" multiple 
 id="project_members">
         <?php foreach($members as $mem){ ?>
            <option value="<?php echo $mem->email;?>"><?php echo $mem->firstname." 
 ".$mem->lastname;?></option>
         <?php }?>
       </select>
   </form>
</div>

你的控制器现在应该是

<?php 
    public function float_project(){
       $members = $this->input->post('project_members');
       foreach($members as $row){
        echo $row;
       } 
    } 
?>

如果你想为每条记录执行插入,你的控制器应该是

<?php 
    public function float_project(){
       $members = $this->input->post('project_members');
       foreach($members as $row){
        $insert = $this->my_model->insertProjectMembers($row);
        if($insert){
           echo "Record were added";
         }
       } 
    } 
?>

你的模型应该是这样的

class My_model extends CI_Model {
    public function insertProjectMembers($row){
      $data = array(
         'db_column_name' => $row
      );
      return $this->db->insert('your_table_name', $data);
    }
}