提问者:小点点

数据库错误:在codeigniter 3中遇到PHP错误


我的模型:

class Admin_model extends CI_Model{
    function add_blog($data){
        $this->db->insert('blog',$data);
        return true;
    }
}

我的控制器功能:

function add_blog(){

    $this->form_validation->set_rules('heading','Heading', 'trim|required');
    if ($this->form_validation->run() == FALSE) {
        $this->session->set_flashdata('error',"Please check the required field");
        redirect('admin/dashboard/add');
    }else{
        //for upload file
        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|txt|jpeg|png|ppt';
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
        }
        else
        {
            $image_path = base_url("uploads/".$post['raw_name'].$post['file_ext']);
            $data['picture']  = $image_path;
            $data_image = array('upload_data' => $this->upload->data());
        }
        //Upload file end
        $data['picture'] = $data_image['upload_data']['file_name']; 
        $data['heading'] = ucwords($this->input->post('heading'));
        $data['description'] = ucfirst($this->input->post('description'));
        $data['type_id'] = ucwords($this->input->post('type_id'));
        $data['price'] = $this->input->post('price');

        $insert_data = $this->admin_model->add_blog($data);
        if($insert_data){
            echo '<script>alert("Note added sucessfully.");</script>';
            redirect('admin/dashboard');
        }else{
            $this->session->set_flashdata('message',"There is problem in inserting data, please retry once");
            echo "error at last ";
            redirect('admin/dashboard/add');
        }
    }
}

我的看法是:

<form role="form" name="myForm"  method="post" enctype="multipart/form-data" action="<?php echo site_url('admin/dashboard/add_blog');?>">
    <label for="">Category</label>
    <select name="type_id" id="" class="form-control" >
        <?php
            if(count($product_list )>0 ){
                foreach ($product_list as $key => $value) {
                ?>
                <option value="<?php echo $value['type_id'];?>"><?php echo $value['type'];?></option>
                <?php
                }}?>
    </select>

    <label for=""> Heading </label>
    <input type="text" name="heading"class="form-control">
    <label for="">Price</label>
    <input type="number" name="price" class="form-control">
    <label for="">Description</label>
    <textarea name="description" id="" cols="30" rows="5" class="form-control"></textarea>
</div>

<div class="form-group">
    <label for="exampleInputFile">Upload pic</label>
    <div class="input-group">
        <div class="custom-file">
            <!--  <input type="file" name="userfile" class="form-control"> -->
            <?php echo form_upload(['name'=>'userfile']); ?>
        </div>
    </div>
    <?php if(isset($upload_error)){echo $upload_error;}?>
</div>
</div>
<div class="card-footer">
    <button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>

遇到一个PHP错误

严重性:通知消息:未定义变量:data_image文件名:admin/Dashboard.php行号:64

回溯:

文件:/opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard。php行:64函数:\u错误\u处理程序

文件: /opt/lampp/htdocs/shremad/index.php行:315功能:require_once

遇到一个PHP错误

严重性:注意消息:尝试访问null类型值的数组偏移量文件名:admin/Dashboard。php行号:64

回溯:

文件:/opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard。php行:64函数:\u错误\u处理程序

文件: /opt/lampp/htdocs/shremad/index.php行:315功能:require_once

发生数据库错误

错误编号:1048列“图片”不能为空

INSERT INTO博客图片标题描述type_id价格)值(NULL,'Djjdjdj','Jjdjdj','1',")

文件名:模型/Admin_model.php行号:5


共1个答案

匿名用户

我们先来分析一下错误信息。

消息:未定义变量:数据\图像

这意味着,在代码的某个地方,你调用了一个名为“data_image”的变量,这是未定义的。现在,请注意,这是一个通知,所以可能会有这样的情况,你的代码可以很好地工作,但这是一个不好的做法,可能会导致麻烦。(比如你的情况)。

查看您的代码,下面的“块”:

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
    }
    else
    {
        $image_path = base_url("uploads/".$post['raw_name'].$post['file_ext']);
        $data['picture']  = $image_path;
        $data_image = array('upload_data' => $this->upload->data());
    }
        //Upload file end
    $data['picture'] = $data_image['upload_data']['file_name']; 

$data\u image变量仅在“else”语句下定义。因此,如果条件返回true,则$data\u image变量实际上是未定义的。在这种情况下,将使用以下命令:

$data['picture'] = $data_image['upload_data']['file_name']; 

它基于$data\u image变量,将返回另一个问题:

消息:正在尝试访问null类型值上的数组偏移量

因此,$data['picture']现在是NULL,这会导致上一个问题:

列“picture”不能为空

解决方案?

即使条件返回true,也要确保有$data_image的值。或者,如果这个条件返回true-以不同的方式处理事情。还请注意,在返回意外结果的条件下,函数可能存在逻辑问题。

(条件:if(!$this)-