首先,我很抱歉我的英语不好,我一直在搜索所有如何解决我的问题(当试图不更新图像时数据库上的图像消失了)我发现了这一点:{{3} }
我的问题与那个问题非常相似,但是当我尝试修复它时仍然是错误。
所以我可以更新图像或将旧图像更改为新图像,但是当我尝试不更新图像时(可能只是编辑表单中的其他值,而不是图像),数据库上的图像值消失了(空值)。我想要的是当我不更新图像时,图像仍然像旧图像一样。这是我的代码,放在模型目录的同一个文件中:
构造函数:
public function __construct()
{
parent::__construct();
// Setting up the upload configuration
$config['upload_path'] = 'photo_dir/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['file_ext_tolower'] = TRUE;
$config['max_size'] = 2048;
$this->load->library('upload', $config);
}
获取旧图片值的功能:
// This function is to read the old image in database
private function _selected_img($id)
{
return $this->db->select()
->from('tb_exam')
->where('id_exam', $id)
->limit(1)
->get()
->row();
}
更新功能
// The update function
public function update($id)
{
// Get the old image first and declare it in variable `file`
$file = $this->_selected_img($id)->img;
// Upload the image
$this->upload->do_upload('img');
// If upload image data is not null, and then change the
// `file` value into the image file_name
if ($this->upload->data() !== '')
{
$file = $this->upload->data('file_name');
}
// Object to update
$this->object = array(
'id_teacher' => $this->input->post('id_teacher'),
'id_subject' => $this->input->post('id_subject'),
'question' => $this->input->post('question'),
'img' => $file,
'option_a' => $this->input->post('option_a'),
'option_b' => $this->input->post('option_b'),
'option_c' => $this->input->post('option_c'),
'option_d' => $this->input->post('option_d'),
'option_e' => $this->input->post('option_e'),
'answer_key' => $this->input->post('answer_key')
);
// Update the data
$this->db->where($this->main_id, $id)->update($this->table, $this->object);
// If record is success, return TRUE
// but if not, return FALSE
if ($this->db->affected_rows() > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
非常感谢您的所有帮助和回答!
答案 0 :(得分:0)
您是否正确使用了旧图像名称。请通过echo $file; exit;
查看
如果您没有正确获取文件名,请使用以下代码获取旧图像名称$query = $this->db->get_where('tb_exam', array('id_exam' => $id));
$data=$query->result_array();
$file=$data[0]['img'];
直接在更新功能中使用此代码