duaj39673 2018-07-07 06:44
浏览 34
已采纳

无法将多选数据存储到数据库

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>
  • 写回答

4条回答 默认 最新

  • douguomou5094 2018-07-07 06:59
    关注

    Change your multiselect part like this.

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

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

    UPDATE

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

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

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

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效