更新记录时面临的问题

I'm trying to update my records and it's working fine, but I'm facing a few issues here trying to fix it (but am unable to do so).

The issue I'm facing is that when I don't want to update my image (say I simply want to update my first name and last name only) then only those should get updated, but instead, when I try to do that, the image name (which is stored in my database) is getting deleted, and there will be is no image available.

So can anyone guide me what I should do please?

Controller part:

                    public function updatedata()
                {
                  $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)
                      {

                          echo validation_errors();
                          $this->load->view('update_records',$result);
                          //exit();
                      } 

                      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');
                                   }
                              else
                                   {
                                      $fi= $result['data']->filename;
                                   }
                              $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
                              echo 'Successfully updated your record';
                              //exit();
                          } 
        //                   else 
        //                   {
        //                       echo $this->upload->display_errors();
        //                       //exit();
        //                   }
                      }

View part:

        <body>
         <?php
          $i=1;
          foreach($data as $row)
          {
          ?>
            <form method="post" enctype="multipart/form-data">
                    <div class="container">

                        <h5 style="color:#ff1a1a">First name</h5> 
                        <input type="text" class="form-control" name="fname" value="<?php echo $row->first_name; ?>"/>
                        <span class="text-danger"><?php echo form_error("fname");?></span>

                       <h5 style="color:#ff1a1a">Last name</h5>
                       <input type="text" class="form-control" name="lname" value="<?php echo $row->last_name; ?>"/>
                       <span class="text-danger"><?php echo form_error("lname");?></span>

                        <h5 style="color:#ff1a1a">Username</h5>
                        <input type="text" class="form-control" name="username" value="<?php echo $row->username; ?>"/>
                        <span class="text-danger"><?php echo form_error("username");?></span>

                        <h5 style="color:#ff1a1a">E-mail</h5>
                        <input type="text" class="form-control" name="email" value="<?php echo $row->email; ?>"/>
                        <span class="text-danger"><?php echo form_error("email");?></span>

                        <h5 style="color:#ff1a1a">Image</h5>
                        <img src="<?php echo base_url('uploads/' . $row->filename);?>" class="img-responsive" alt="image" width="100px" height="100px"><br><input type="file" name="filename" value="<?php echo $row->filename; ?>">

                        <br><input type="submit" name="update" class="btn btn-success" value="Update Records"/>

                    </div>    
            </form>
            <?php } ?>
        </body>

model part

        //get values 
    function displayrecordsById($id)
{
        $query=$this->db->query("select * from form where ID='".$id."'");
        return $query->result();
}

    //update record
    function updaterecords($fn,$ln,$un,$em,$fi,$id)
{
        $query=$this->db->query("update form SET first_name='$fn',last_name='$ln',username='$un',email='$em',filename='$fi' where ID='".$id."'");
}

Please try by the code give below:

function updaterecords($fn = '', $ln = '',$un = '', $em = '', $fi = '', $id = '')
{
    $data = array();
    if (isset($fn) && !empty($fn))
        $data['first_name'] = $fn;
    if (isset($ln) && !empty($ln))
        $data['last_name'] = $ln;
    if (isset($un) && !empty($un))
        $data['username'] = $un;
    if (isset($em) && !empty($em))
        $data['email'] = $em;
    if (isset($fi) && !empty($fi))
        $data['filename'] = $fi;
    if (!empty($data) && !empty($id))
        $this->db->update('form', $data, array('ID' => $id)); 
}