Search code examples
phpcodeignitercodeigniter-3

error while updating my records


This is my controller :

        public function updatedata($id)
                {
                  //$id=$this->input->get('id');
                  $result['data']=$this->Form_model->displayrecordsById($id);

                      $this->form_validation->set_rules('fname', 'First name', 'required');
                      $this->form_validation->set_rules('lname', 'Last name', 'required');
                      $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
                      $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

                      if ($this->form_validation->run() == FALSE)
                      {
                          $this->load->view('update_records',$result);
                      } 

                      else 
                      {
                          $config['upload_path']   = './uploads/';
                          $config['allowed_types'] = 'gif|jpg|png';
                          $this->load->library('upload', $config);

                              $fn=$this->input->post('fname');
                              $ln=$this->input->post('lname');
                              $un=$this->input->post('username');
                              $em=$this->input->post('email');
                              if($this->upload->do_upload('filename'))
                                   {
                                       $fi= $this->upload->data('file_name');
        //                               $file = ("uploads/".$result['data']->filename);
        //                                //delete the existing file if needed
        //                                if (file_exists($file)) {
        //                                    unlink($file);
        //                                }
                                   }
                              else
                                   {
                                      $fi= $result['data']->filename;
                                   }
                              $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
                              echo 'Successfully updated your record';
                              //exit();
                          } 
                      }

im trying to update the data which is already stored in the database, its working fine but the issue im facing is that when i don want to update image section, say only if i want to update the first name or just email, its also working good but later when i see in my database the image file name is deleted and thus the image is also not getting displayed.

the error im facing is this :

A PHP Error was encountered Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/Form.php

Line Number: 146

Backtrace:

File: C:\xampp\htdocs\amit\application\controllers\Form.php Line: 146 Function: _error_handler

File: C:\xampp\htdocs\amit\index.php Line: 315 Function: require_once

please can any one help me out please im trying to fix this from yesterday but im unable to do so please can any one help me please


Solution

  • Hope this will help you :

    First: Replace it

    $fi= $result['data']->filename;
    

    With this

     $fi= $result['data'][0]->filename;
    

    Second : To Replace the existing image you should add $config['overwrite']=TRUE; in your config like this :

    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['overwrite'] = TRUE;
    $this->load->library('upload', $config);
    /*........your other code .........*/
    

    For more : https://www.codeigniter.com/user_guide/libraries/file_uploading.html#preferences