简体   繁体   English

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

[英]How to insert only one image into database codeigniter?

Hi all I am a new of codeigniter I have problam with insert image into database I have 3 image form upload . 大家好,我是一个新的codeigniter,我有将图像插入数据库的问题,我上传了3张图像。 When I insert image 1 ,2 and 3 into database it's ok. 当我将图像1,2和3插入数据库时​​就可以了。 nut when I insert image 1 into database and image 2 and 3 is empty it has the error is : 当我将图像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

how can I do it .this is my code in controller: 我该怎么办。这是我在控制器中的代码:

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

Try this code.single image upload in codeigniter 3.0.1 试试这个代码。在codeigniter 3.0.1中上传单个图像

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

                }
            }
        }

Add error_reporting(0) in your function burger_add() to remove notice error. 在您的函数burger_add()添加error_reporting(0)以删除通知错误。

Like this - 像这样 -

function burger_add(){

    error_reporting(0);

 # here write your code

}

And you have to turn on output buffering in your constructor 而且您必须在构造函数中打开输出缓冲

public function __construct()
{
    ob_start();

}

After adding output buffering you've never encountered the message "Warning: Cannot modify header information - headers already sent by (output)" . 添加输出缓冲后,您再也不会遇到消息“警告:无法修改标头信息-已由(output)发送的标头”。

ATTENTION: Suppressing errors or temporary disabling error reporting does NOT solve the problem, it just hides the error / warning message. 注意:抑制错误或暂时禁用错误报告并不能解决问题,只是隐藏了错误/警告消息。

I am not familiar with CI but do_upload sounds not like a method that could be used to check if a upload item exists. 我不熟悉CI,但do_upload听起来不像一种可用于检查上传项目是否存在的方法。

Edit: i have discovered the CI docs a bit. 编辑:我发现了CI文档有点。 It is indeed the method to check if a specific upload field does ( or uploads in general does ) exists. 实际上,这是检查特定上传字段是否存在(或通常存在上传)的方法。

So all you have to do is to check if a specific field exists ( along the do_upload(fieldname) method ) and execute the upload task inside the if-body for the specific field, thats all. 因此,您要做的就是检查特定字段是否存在(沿着do_upload(fieldname)方法),并在if-body内部为特定字段执行上载任务,仅此而已。

Its hard to define that source by hand on a smartphone, but i think you can easily define the source by yourself. 很难在智能手机上手动定义该来源,但是我认为您可以轻松地自行定义该来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM