简体   繁体   English

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

[英]How can i store multiple select box value in database in 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>

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. 而不是需要将列数据类型更改为varchar或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 参考链接: http : //www.w3schools.com/php/func_string_implode.asp

You should use 'user_id'=>implode(',',$this->input->post('user_id')); 您应该使用'user_id'=>implode(',',$this->input->post('user_id')); to convert your array into comma separated value. 将您的数组转换为逗号分隔的值。 And after that you can store it into database table. 然后,您可以将其存储到数据库表中。 Because you can not directly stores the array into database table 因为您不能直接将数组存储到数据库表中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从下拉选择框列表中将用户选择的选项的值存储到mysql数据库中? - How can I store the value of a user selected option from a drop down select box list into mysql database? 如何在codeigniter的选择框中显示从数据库中选择的值 - how to show the selected value from database in select box in codeigniter 如何在具有多个数据库的CodeIgniter中存储数据库会话? - How do I store database sessions in CodeIgniter with multiple databases? 如何使用带有 codeigniter 的 Select2 插件将多个数据插入到我的数据库中? - How can I insert multiple data to my database using the Select2 plugin with codeigniter? 在Codeigniter中将多个选择值插入数据库 - Inserting Multiple Select Value to database in Codeigniter 如何将数据库的多个表值提取到动态选择框中 - how to fetch multiple tables value of database into dynamic select box 如何将多个 select 框数据保存到数据库中,例如 Laravel 格式的 json 格式 - How can I save multiple select box data to database like json format in Laravel 如何将多个选择框ID中的数据插入数据库 - how can I insert data from multiple select box ID into database 如何在PHP中将未经检查的框数据存储在数据库中? - How can I store Unchecked box data in database in PHP? 如何在没有刷新页面的情况下根据选择框值从codeigniter中获取数据库中的值? - how to get values from database in codeigniter based on select box value without refreshing page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM