dongzhao5970 2016-02-22 06:58
浏览 36

codeigniter将多个图像插入到不同的数据库字段中

if i insert into database i want two image to be inserted in to different fields in the database example image1_name image2_name first image here second image here i only know how to insert one

controller:

function do_upload()
{
    $config['upload_path'] = './assets/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2000';
    $config['max_width'] = '2000';
    $config['max_height'] = '2000';
    $config['new_image'] = './assets/';

    $config['overwrite'] = TRUE;
    $this->load->library('upload', $config);
    $this->form_validation->set_rules('name', 'Category Name', 'required');
    if (!$this->upload->do_upload() || !$this->form_validation->run()) {
        $error = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata("message2","Product not added");
        redirect('add_products','refresh');
    } else {

        $data = $this->upload->data();
        $this->thumb($data);


        $file = array(
            'img_name' => $data['raw_name'],
            'thumb_name' => $data['raw_name'] . '_thumb',
            'ext' => $data['file_ext'],
            'category' =>$this->input->post('name')


            );
        $data = array('upload_data' => $this->upload->data());

        if($this->User->insert_cat($file) === TRUE)
        {
            $this->session->set_flashdata("message","You Have Successfully Added a new Category!");
            redirect('add_category','refresh');
        }else
        {
            $this->session->set_flashdata("message2","Category not added");
            redirect('add_category','refresh');
        }
    }
}

view:

<div class="panel panel-default">
    <div class="panel-body">
        <?php echo form_open_multipart('add_new_category/do_upload');?>
        <center>
        <?php if (validation_errors()): ?>

            <div class="alert alert-danger alert-dismissible" role="alert" style="width: 700px;">
                <?php echo validation_errors(); ?>
            </div>
        <?php endif ?>


        <div class="form-group">
            <label class="col-sm-2 control-label" style=" color: white"></label>

            <label class="col-sm-2 control-label">Category Image</label>
            <div class="col-sm-5">
                <input type="file" class="form-control" placeholder="" name="userfile">
            </div>

        </div>
        <br> <br> <br>
        <div class="form-group">
            <label class="col-sm-2 control-label" style=" color: white"></label>

            <label class="col-sm-2 control-label">Category Name</label>
            <div class="col-sm-5">
                <input type="text" class="form-control" placeholder="ex. coffee"
                name="name"
                value="<?php echo set_value('name'); ?>">
            </div>

        </div>

model:

public function insert_cat($file)
{
    // $this->db->insert('product_table',$file);
    if($this->db->insert('product_category', $file))
    {
        return TRUE;
    }else
    {
        return FALSE;
    }
}
  • 写回答

1条回答 默认 最新

  • dpfln86244 2016-02-22 07:23
    关注

    Add one more function in model

        public function insert_cat1($file){
                $this->db->insert('product_table',$file);
    
               {
                    return TRUE;
               }else
               {
                  return FALSE;
              }
            } 
    
    And change the controller
    
    
        function do_upload() {
                $config['upload_path'] = './assets/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2000';
                $config['max_width'] = '2000';
                $config['max_height'] = '2000';
                $config['new_image'] = './assets/';
    
                $config['overwrite'] = TRUE;
                $this->load->library('upload', $config);
                $this->form_validation->set_rules('name', 'Category Name', 'required');
                if (!$this->upload->do_upload() || !$this->form_validation->run()) {
                    $error = array('error' => $this->upload->display_errors());
                    $this->session->set_flashdata("message2","Product not added");
                 redirect('add_products','refresh');
                } else {
    
                    $data = $this->upload->data();
                    $this->thumb($data);
    
    
                    $file = array(
                        'img_name' => $data['raw_name'],
                        'thumb_name' => $data['raw_name'] . '_thumb',
                        'ext' => $data['file_ext'],
                        'category' =>$this->input->post('name')  
    
    
                    );
                      $data = array('upload_data' => $this->upload->data());
                     $insert1=$this->User->insert_cat($file);
                     $insert2=$this->User->insert_cat1($file);
                   if(($insert1)&&($insert2)==TRUE)
             {
                $this->session->set_flashdata("message","You Have Successfully Added a new Category!");
                redirect('add_category','refresh');
             }else
             {
                 $this->session->set_flashdata("message2","Category not added");
                 redirect('add_category','refresh');
              }
              }
              }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器