无法将多选数据存储到数据库 [英] unable to store the multiselect data to database

查看:52
本文介绍了无法将多选数据存储到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图将值存储在数据库中,所有值都存储在数据库中,但问题出在下拉列表中。当我尝试仅上传多选下拉列表
时,最后一个选定值将被存储,因为我要将所有选定值存储在数据库中,有人可以指导我如何做到这一点

im trying to store the values in database, all the values are getting dtored in the database but the issue is with dropdown. when i try to upload the multiselect dropdown only the last selected value is getting stored, where as i want to store all the selected values in the database, can some one guide me how can i do that

    //----controller-----
    public function portfolio1()
        {  
            $this->form_validation->set_rules('first_content', 'First content', 'required');
            $this->form_validation->set_rules('second_content', 'Second content', 'required');
            $this->form_validation->set_rules('type', 'Type', 'required');

            if ($this->form_validation->run()==TRUE)
            {
                $config['upload_path']   = FCPATH.'uploads/'; 
                $config['allowed_types'] = 'gif|jpg|png'; 
                $this->load->library('upload', $config);

                if ( $this->upload->do_upload('filename') )
                {
                    //print_r($this->upload->data());die; 
                    $data['first_content'] = $this->input->post('first_content');
                    $data['second_content'] = $this->input->post('second_content');
                    $data['type'] = $this->input->post('type');
                    $data['filename'] = $this->upload->data('file_name');  

                    //Transfering data to Model
                    $this->Contact_model->portfolio1($data);
                    //Redirecting to success page
                    redirect(site_url('Home/portfolio1'));     
                }
                else
                {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);die;
                }
            }
            else
            {
                  $this->load->view('portfolio1'); 

            }
        } 


        //----model----------
        function portfolio1($data)
        {
            //saving records
            $this->db->insert('portfolio1', $data); 
        }


     //------view page-----


   <?php echo form_open_multipart('Home/portfolio1'); ?>
    <div style="padding-top:10%; padding-left:30%">
        <div>
            <textarea name="first_content" rows="4" cols="50"></textarea>
            <span><?php echo form_error("first_content");?></span>
        </div><br>
        <div>
            <textarea name="second_content" rows="4" cols="50"></textarea>
            <span><?php echo form_error("second_content");?></span>
        </div><br>
        <div>
            <select name="type" multiple>
                <option value="*">All Works</option>
                <option value=".bootstrap">Creative</option>
                <option value=".html">Photography</option>
                <option value=".wordpress">Web Development</option>
            </select>
            <span><?php echo form_error("type");?></span>
        </div><br>
        <div>
            <input type="file" name="filename">
            <span><?php echo form_error("filename");?></span>
        </div><br>
      <button type="submit" class="btn btn-default">Submit</button>
    </div>
</form>


推荐答案

像这样更改多选零件。

<select name="type[]" multiple>
    <option value="*">All Works</option>
    <option value=".bootstrap">Creative</option>
    <option value=".html">Photography</option>
    <option value=".wordpress">Web Development</option>
</select>

我已将名称从 type 更改为 type [] ,这样您将获得所有选定的选项。

I have change the name from type to type[] so you will get all selected option.

更新

if ( $this->upload->do_upload('filename') )
            {
                //print_r($this->upload->data());die; 
                $data['first_content'] = $this->input->post('first_content');
                $data['second_content'] = $this->input->post('second_content');
                $data['type'] = implode(",",$this->input->post('type'));
                $data['filename'] = $this->upload->data('file_name');  

                //Transfering data to Model
                $this->Contact_model->portfolio1($data);
                //Redirecting to success page
                redirect(site_url('Home/portfolio1'));     
            }

现在它将保存 type 以逗号分隔在表格中。

Now it will save type as comma separated in your table.

这篇关于无法将多选数据存储到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆