【问题标题】:codeigniter image update代码点火器图像更新
【发布时间】:2012-11-04 22:45:50
【问题描述】:

我遇到了图片上传器的问题。我已经创建了工作正常的图像上传器,但我还需要编辑它们。 当我添加需要的图像时,db 列会正确更新,但如果我不更改图像并保持原样,我会收到错误 "Column 'image' cannot be null"

这是更新部分的代码:

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

对不起,如果我不是很明确

编辑:

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

【问题讨论】:

  • 你在echo var_dump($this->input->post('current_image'))得到了什么?
  • @GBD 如果我在 if($this->input->post('image')) { } 中使用它,我会从 db 中获取值,但如果我在 else 中使用它 {} 我不会'什么都得不到
  • 你能在其他部分回显“你好”吗?没有图像文件时看它执行与否
  • 这意味着 .it 总是进入 if 部分。在 if 部分执行:$data['image'] = !empty($fdata['upload_data']['file_name'])?$fdata['upload_data']['file_name']:$this->input->post('current_image');
  • 如果 echo $data['image'] 会得到什么?

标签: php codeigniter


【解决方案1】:

根据我们的 cmets 线程,您需要在此处更改代码

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

第二个问题:A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: libraries/Log.php Line Number: 99

变化:

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

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

【讨论】:

  • 当我不插入任何图像时,相同的 php 通知。 A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: libraries/Log.php Line Number: 99
  • 我认为这个问题在 Log.php 中
  • 在第 99 行的 Log.php 我有 $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
  • 新错误:)。 Severity: Notice Message: Undefined variable: data Filename: controllers/add.php Line Number: 257 第 257 行是 return $data;
  • 把这个 $data = array();之前if ( ! $this->upload->do_upload('image'))
猜你喜欢
  • 2011-10-07
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
相关资源
最近更新 更多