rajhem
rajhem

Reputation: 1

database error: A PHP Error was encountered in codeigniter 3

My model:

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

My controller function:

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

My view:

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

A PHP Error was encountered

Severity: Notice Message: Undefined variable: data_image Filename: admin/Dashboard.php Line Number: 64

Backtrace:

File: /opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard.php Line: 64 Function: _error_handler

File: /opt/lampp/htdocs/shremad/index.php Line: 315 Function: require_once

A PHP Error was encountered

Severity: Notice Message: Trying to access array offset on value of type null Filename: admin/Dashboard.php Line Number: 64

Backtrace:

File: /opt/lampp/htdocs/shremad/application/controllers/admin/Dashboard.php Line: 64 Function: _error_handler

File: /opt/lampp/htdocs/shremad/index.php Line: 315 Function: require_once

A Database Error Occurred

Error Number: 1048 Column 'picture' cannot be null

INSERT INTO blog (picture, heading, description, type_id, price) VALUES (NULL, 'Djjdjdj', 'Jjdjdj', '1', '')

Filename: models/Admin_model.php Line Number: 5

Upvotes: 0

Views: 802

Answers (1)

Ofir Baruch
Ofir Baruch

Reputation: 10346

Let's analyze the error message first.

Message: Undefined variable: data_image

Which means, somewhere in the code you're calling a variable named "data_image" which is undefined. Now, please notice that this is a NOTICE, so there might be cases that your code would work well BUT it's a bad practice and might cause troubles. (As in your case for instance).

Looking at your code, the following "block":

    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']; 

The $data_image variable is defined only under the "else" statement. So if the condition returns true, the $data_image variable is truly undefined. In that case, the following command:

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

which is based on the $data_image variable, would return another problem:

Message: Trying to access array offset on value of type null

so $data['picture'] now is NULL, which cause your last problem:

Column 'picture' cannot be null

The solution?

Make sure you have a value for $data_image even in the case that the condition returns true. Or, if this condition returns true - handle the things differently. Please also notice that there might be a logical problem with the function in the condition that returns an unexpected result.

(The condition: if ( ! $this->upload->do_upload()))

Upvotes: 1

Related Questions