dongrong7883 2017-02-07 06:00
浏览 33
已采纳

如何在codeigniter中的数据库中存储多个选择框值?

I have one requirements form in my page..in that form i have users dropdown with select box. I want to select multiple users at a time and need to store selected users in database.

Controller:

public function requirement()
{
    $this->load->model('LoginModel');
    $data['user'] = $this->LoginModel->getusers();
    $this->load->view('Admin/requirements',$data);

    $insert = array (
      'role_name'             => $this->input->post('role_name'),
      'vacancies'             => $this->input->post('vacancies'),
      'experience'            => $this->input->post('experience'),
      'jd'                    => $this->input->post('jd'),
      'hiring_contact_name'   => $this->input->post('hiring_contact_name'),
      'hiring_contact_number' => $this->input->post('hiring_contact_number'),
      'user_id'               => $this->input->post('user_id')//this is my foreign key id from users table);
    );
    $this->LoginModel->add_requirement($insert);
}

Model:

function getusers()
{
    $this->db->select('*');
    $this->db->from('users');
    $query = $this->db->get();
    //echo $this->db->last_query();
    return $query->result();
}

View page

<div class="form-group">
    <label>Choose Vendor</label>
    <select id="chkveg"class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >
        <option value="0"></option>
        <?php
        print_r($user);
        foreach ($user as $rows)
        {
            ?>
            <option value="<?php echo $rows->user_id ?>">
                <?php echo ucfirst($rows->first_name) ?>
            </option>
            <?php
        }
        ?>
    </select>
</div>
  • 写回答

2条回答 默认 最新

  • dongzhuo3059 2017-02-07 06:13
    关注

    make select-box array instead of single value.

    <select id="chkveg""  class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >
    

    than you need to ALTER your column datatype to varchar or text.

    after that you can implode function to create string value like

    'user_id'=> implode(',',$this->input->post('user_id')); //eg.1,2,3
    

    Reference link : http://www.w3schools.com/php/func_string_implode.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个bat,根据文件前缀创建文件夹,并将文件移动到对应前缀名称的文件夹内
  • ¥15 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内