如何只将一个图像插入数据库codeigniter?

时间:2015-11-12 04:10:04

标签: php codeigniter codeigniter-3

大家好我是一个新的codeigniter我有一个插入图像到数据库的problam 我有3张图片表单上传。当我将图像1,2和3插入数据库时​​,没关系。当我将图像1插入数据库并且图像2和3为空时,它的错误是:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: img2

Filename: mbl-admin/Site_admin.php

Line Number: 153

Backtrace:

File: /home/wwwmyburgerlabco/public_html/mbl/application/controllers/mbl-admin/Site_admin.php
Line: 153
Function: _error_handler

File: /home/wwwmyburgerlabco/public_html/mbl/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: img3

Filename: mbl-admin/Site_admin.php

Line Number: 154

Backtrace:

File: /home/wwwmyburgerlabco/public_html/mbl/application/controllers/mbl-admin/Site_admin.php
Line: 154
Function: _error_handler

File: /home/wwwmyburgerlabco/public_html/mbl/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/wwwmyburgerlabco/public_html/mbl/system/core/Exceptions.php:272)

Filename: helpers/url_helper.php

Line Number: 564

Backtrace:

File: /home/wwwmyburgerlabco/public_html/mbl/application/controllers/mbl-admin/Site_admin.php
Line: 157
Function: redirect

File: /home/wwwmyburgerlabco/public_html/mbl/index.php
Line: 292
Function: require_once

我该怎么做。这是我在控制器中的代码:

function burger_add(){
    $config['upload_path']          = './images/burger/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 3500;
        $config['max_width']            = 4800;
         $config['max_height']           = 3200;
        $this->load->library('upload', $config);
        if($this->upload->do_upload('userfile2'))
        {
           $img2 = $this->upload->data();
        }
        if($this->upload->do_upload('userfile3'))
        {
           $img3 = $this->upload->data();
        }
            $this->load->library('form_validation');
            $this->form_validation->set_rules('title','Title','required|trim');
              $this->form_validation->set_rules('category','Category','required|trim');
                if ( ! $this->upload->do_upload('userfile1') || ($this->form_validation->run() == false))
                {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('admin_view/header_admin');
                $this->load->view('admin_view/burger_add',$error);
                $this->load->view('admin_view/footer_admin');
                }
                else{
                  $img1 = $this->upload->data();
                    $insert = array(
                      'category' => $this->input->post('category'),
                      'title' => $this->input->post('title'),
                      'description' => $this->input->post('description'),
                      'image' => $img1['file_name'],
                       'image2' => $img2['file_name'],
                      'image3' => $img3['file_name'],
                      );
                    $this->db->insert('product',$insert);
                        redirect('mbl-admin/Site_admin/burger_add_form');
                    }
            }

3 个答案:

答案 0 :(得分:1)

  

在codeigniter 3.0.1

中试用此code.single图片上传
function burger_add(){

    $config['upload_path']          = './images/burger/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 3500;
        $config['max_width']            = 4800;
         $config['max_height']           = 3200;

            $config['remove_spaces'] = true;
            $this->load->library('upload', $config);

            if (!$this->upload->do_upload('userfile2')) 
            { 
                $error = ['error' => $this->upload->display_errors()];
                $this->load->view('admin_view/burger_add', $error);
            }
            else
            {

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

                foreach ($uploaddata as $value)
                {

                    $data = [
                                'name' => $value['file_name'],
                                'type' => $value['file_type'],
                                'category' => 'news'
                            ];
                    $this->Image_model->image($data);

                }
            }
        }

答案 1 :(得分:1)

在您的函数error_reporting(0)中添加burger_add()以删除通知错误。

喜欢这个 -

function burger_add(){

    error_reporting(0);

 # here write your code

}

你必须在构造函数中打开输出缓冲

public function __construct()
{
    ob_start();

}

添加output buffering后,您从未遇到过消息"警告:无法修改标头信息 - 已经由(输出)"发送的标头

注意:抑制错误或临时禁用错误报告并不能解决问题,它只是隐藏错误/警告消息。

答案 2 :(得分:0)

我不熟悉CI,但 do_upload 听起来不像是一种可用于检查上传项目是否存在的方法。

编辑:我发现了一些CI文档。确实存在检查特定上载字段是否存在(或一般上载确实存在)的方法。

所以你要做的就是检查是否存在特定字段(沿着do_upload(fieldname)方法)并在if-body中为特定字段执行上传任务,这就是全部。

很难在智能手机上手动定义该来源,但我认为您可以自己轻松定义来源。