2

I am working with image updating api in codeigniter,If i update my image then function is working fine but if i not uploading any image then image field become "Null" in database.Here is my code

$add_data['user_id'] = ($this->input->post('user_id') && !empty($this->input->post('user_id'))) ? $this->input->post('user_id') : NULL;

if(!file_exists($_FILES['resume']['tmp_name']) || !is_uploaded_file($_FILES['resume']['tmp_name'])) 
                        {
                            $add_data['resume'] = ($this->input->post('resume') && !empty($this->input->post('resume'))) ? $this->input->post('resume') : NULL;

                            $add_data['resume']=$add_data['resume'];
                        }
            else
                        {
                            $filename=time().uniqid(rand()).$_FILES['resume']['name'];
                            move_uploaded_file($_FILES["resume"]["tmp_name"], "resume/" . $filename);
                            $add_data['resume']=$filename;
                        }

                $data = array(
                'resume'=>$add_data['resume'],
                );  
                $this->db->where('id',$add_data['user_id']);
                $query=$this->db->update('users_info', $data);

1 Answer 1

1

$filename is not getting any value and in the database it might have default value NULL.

You need to remove/comment this line:

...
 $add_data['resume']=$filename; // <-- $filename is not getting any value,  uncomment or remove it.
}
else
{    
...
Sign up to request clarification or add additional context in comments.

4 Comments

i just want that if i not uploaded any new image then old image should not delete from db
@amitsharma, if you do not specify the field in edit array, it will not update the old value.
Answer updated. If file is not uploaded, we don't need to update value in database.
you are right but my value in database become null automatically,how i can fix this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.