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

Php codeigniter将多个图像插入不同的数据库字段,php,database,codeigniter,Php,Database,Codeigniter,如果我插入到数据库中,我希望在数据库示例中的不同字段中插入两个图像 图像1\u名称图像2\u名称 第一张图片在这里第二张图片在这里 我只知道如何插入一个 控制器: function do_upload() { $config['upload_path'] = './assets/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '2000'; $config['max_width'

如果我插入到数据库中,我希望在数据库示例中的不同字段中插入两个图像 图像1\u名称图像2\u名称 第一张图片在这里第二张图片在这里 我只知道如何插入一个

控制器

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');
        }
    }
}
<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>
public function insert_cat($file)
{
    // $this->db->insert('product_table',$file);
    if($this->db->insert('product_category', $file))
    {
        return TRUE;
    }else
    {
        return FALSE;
    }
}
查看

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');
        }
    }
}
<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>
public function insert_cat($file)
{
    // $this->db->insert('product_table',$file);
    if($this->db->insert('product_category', $file))
    {
        return TRUE;
    }else
    {
        return FALSE;
    }
}

在模型中再添加一个函数

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

我只能在您的视图中看到一个图像输入文件。。。。您想从同一个输入文件上载多个图像..?可能的重复图像尚未尝试多次上载,因此它只有一个图像,我想将两个图像分别上传到数据库中的两个不同字段不适用于我它只需将第一个图像添加到两个字段在两个表中插入$file它们位于同一个表中,只是具有不同的字段