【问题标题】:How to insert only one image into database codeigniter?如何仅将一张图像插入数据库codeigniter?
【发布时间】:2015-11-12 04:10:03
【问题描述】:

大家好,我是 codeigniter 的新手,我在将图像插入数据库时​​遇到问题 我有 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');
                    }
            }

【问题讨论】:

  • 有些时候我只有 1 张图片要上传,有时我有 2 张图片或 3 张图片。这取决于 imgae 。我上传了 3 张图片就可以了。但是当我上传 1 或 2 张图片时出现错误
  • 你给我一些代码并帮助我好吗?
  • 是的,有时我需要将第一张图片插入数据库,然后插入所有空图片
  • 是的兄弟也在数据库中
  • 有时我需要上传 2 张图片插入到数据库,这取决于我我有 1 或 2 或 3 张图片

标签: php codeigniter codeigniter-3


【解决方案1】:

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

像这样 -

function burger_add(){

    error_reporting(0);

 # here write your code

}

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

public function __construct()
{
    ob_start();

}

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

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

【讨论】:

  • 我添加 error_reporting(0) 没问题,但是当我添加 public function __construct() { ob_start(); } 它有错误
  • 遇到 PHP 错误 严重性:通知消息:未定义属性:Site_admin::$session 文件名:mbl-admin/Site_admin.php 行号:16
【解决方案2】:

在codeigniter 3.0.1中试试这个code.single image upload

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 张图片要上传,有时我有 2 张图片或 3 张图片。这取决于 imgae 。我上传了 3 张图片没问题。但是当我上传 1 或 2 张图片时出现错误
  • 我觉得你需要多张图片上传功能
  • 3张图片上传到数据库?
  • 有时 1 图像有时 2 有时 3 这取决于我有图像
【解决方案3】:

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

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

所以你所要做的就是检查一个特定的字段是否存在(沿着 do_upload(fieldname) 方法)并在 if-body 中执行特定字段的上传任务,仅此而已。

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

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多