codeigniter image update

徘徊边缘 提交于 2019-12-23 21:12:24

问题


I'm stuck on a problem with image uploader. I've created the image uploader which works fine but I also need to edit them. When I add a need image the db column updates correctly but I if don't change the image and leave it as it is, I get an error "Column 'image' cannot be null"

This is the code for the update section:

else if ($type == 'update')
{

    if($this->input->post('image')) {
        $fdata = $this->savenew();
        $data['image'] = $fdata['upload_data']['file_name'];

    } else {
         $data['image'] = $this->input->post('current_image');


    }

    $return = $this->add_radio_model->update($id, $data);
}

Sorry if I wasn't very explicit

EDIT:

     private function savenew(){                

     $config['upload_path'] = './assets/'; //Make SURE that you chmod this directory to 777!
     $config['allowed_types'] = 'gif|jpg|png';
     $config['max_size']    = '0'; // 0 = no limit on file size (this also depends on your PHP configuration)
     $config['remove_spaces']=TRUE; //Remove spaces from the file name

     $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('image'))
    {
            $data['error']= array('error' => $this->upload->display_errors());
            log_message('error',$data['error']);
            }
            else
                {
                $data = array('upload_data' => $this->upload->data());

                }
        return $data;
    }

回答1:


As per our comments thread, here you need to change your code

else if ($type == 'update')
{
    if($this->input->post('image')) {
        $fdata = $this->savenew();
        // first always set current image  
        $data['image'] = $this->input->post('current_image');
        // second check if new image added if yes, then update otherwise keep original image as it is
        if(isset($fdata['upload_data'])){
           $data['image'] = $fdata['upload_data']['file_name'];
        }
    }
    $return = $this->add_radio_model->update($id, $data);
}

For second problem: A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: libraries/Log.php Line Number: 99

Change:

$data['error']= array('error' => $this->upload->display_errors());
log_message('error',$data['error']);

TO

log_message('error',(string) $this->upload->display_errors());


来源:https://stackoverflow.com/questions/13218842/codeigniter-image-update

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!