0

我正在尝试将 2 个变量传递给表单页面。

第一个是对表单字段的查询,第二个是错误变量。

加载页面时,出现以下错误:

遇到 PHP 错误

严重性:通知
消息:未定义变量:错误
文件名:views/upload_form.php
行号:7

这是我的控制器:

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->database();

    }

    function index()
    {
        $this->load->helper('url');
        $this->load->model('Image_model');

        $data['query'] = $this->Image_model->image_biz();

        $this->load->view('upload_form', $data, array('error' => ' ' ));
    }

    function do_upload()
    {

        $config['upload_path'] = dirname($_SERVER["SCRIPT_FILENAME"]).'/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);

        } else {

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

            // can be add extra feature here.

            $insert_data = array(
                    'image_file' => $image_data['upload_data']['file_name'],
                    'image_path' => $image_data['upload_data']['full_path'],
                    'image_ext' => $image_data['upload_data']['file_ext'],
                    'image_width'=> $image_data['upload_data']['image_width'],
                    'image_height' => $image_data['upload_data']['image_height'],
                     );

            $this->db->insert('image', $insert_data);//load array to database

            $this->load->view('upload_success', $image_data);

        }

    }
}
?>

这是我的视图页面“upload.php”:

    <html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

Image: <input type="file" name="userfile" size="20" /><br>
<label for="select">Business ID:</label>
<select name="image_bizID">
    <?php foreach ($query->result() as $row) { ?>
        <option value=""><?=$row->biz_id;?></option>
    <?php } ?>
</select>

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

任何帮助解决此错误将不胜感激。

谢谢你。

4

2 回答 2

3

Undefined variable: error表示有一个error未定义的名为的变量。

在您看来,您有这一行:

<?php echo $error;?>

这需要$image_data['error']在您的控制器中设置,因为$image_data['error']在控制器中变为$error在视图中。

要修复它最简单的方法,只需将其分配给控制器中的空字符串:

$image_data['error'] = '';

或者只是检查它是否首先在视图中设置:

<?php 
if (isset($error))
    echo $error;
?>

还有一些方法可以通过适当地设置来忽略通知,或者只是在视图error_reporting中像这样抑制该行上的错误:

<?php @echo $error; // notice @ before echo ?>

但解决问题总是比忽略它好。


更新

刚刚注意到,您以错误的方式传递变量。所以这:

function index()
{
    $this->load->helper('url');
    $this->load->model('Image_model');

    $data['query'] = $this->Image_model->image_biz();

    $this->load->view('upload_form', $data, array('error' => ' ' ));
}

应该:

function index()
{
    $this->load->helper('url');
    $this->load->model('Image_model');

    $data['query'] = $this->Image_model->image_biz();
    $data['error'] = '';

    $this->load->view('upload_form', $data);
}

您传递的所有内容都应该在单个数组或对象中,所以$data['query']and$data['error']是正确的方式,因为我们只发送$data数组。加载视图函数中的第三个参数用于将视图作为字符串返回而不是渲染它,所以在这种情况下你不想使用它。

在此处阅读有关视图的更多信息:http: //ellislab.com/codeigniter/user-guide/general/views.html

于 2014-07-20T19:06:39.703 回答
0

只需在索引函数中更改这些行

$data['error']='';
$this->load->view('upload_form', $data); 

您可以选择传递第三个参数,它是布尔数据。

或者您可以只使用 isset 函数来检查错误变量是否已设置

参考链接: http: //php.net/manual/en/function.isset.php

于 2014-12-30T15:27:17.353 回答