如何在 CodeIgniter 中更新图像
How to update image in CodeIgniter
我是 codeigniter 的新手。
我正在为这个项目使用 codeigniter。
我不知道如何更新数据库中的图像表单数据。
我已经插入,显示数据库中的数据已完成。
但我不明白如何更新数据库中的图像。
这是我用来更新现有图像的方法,但我无法
更新图像。
我已成功将图片上传到我的数据库中。
public function update($id) {
$this->form_validation->set_rules('tite', 'title', 'required');
$this->form_validation->set_rules('slug', 'Slug', 'required');
if ($this->form_validation->run() === FALSE) {
$data['category'] = $this->category_model->all();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
if (!empty($_FILES['userfile']['name'])) {
// upload featured image
$config['upload_path'] = './uploads/page';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1080';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$data['page'] = $this->page_model->find($id);
$data['category'] = $this->category_model->all();
$data['error'] = $this->upload->display_errors();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
$data = array('upload_data' => $this->upload->data());
$featured_img = $_FILES['userfile']['name'];
var_dump($data);
var_dump($featured_img);
}
} else {
$this->page_model->update($featured_img, $id);
redirect('backend/page', 'refresh');
}
}
}
这是我用来更新现有图像的方法,但我无法更新图像。
这是我发送数据的模型。
我已成功将图像上传到我的数据库中。
public function update($featured_img, $id) {
$data = array(
'title' => ucwords($this->input->post('title')),
'slug' => strtolower($this->input->post('slug')),
'content' => $this->input->post('content'),
'featured_img' => $featured_img,
'category_id' => $this->input->post('category_id')
);
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
试试下面的代码,它会更新 featured_img
仅当它包含数据时(如果用户上传图片)。
控制器:
public function update($id) {
$this->form_validation->set_rules('tite', 'title', 'required');
$this->form_validation->set_rules('slug', 'Slug', 'required');
if ($this->form_validation->run() === FALSE) {
$data['category'] = $this->category_model->all();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
if (!empty($_FILES['userfile']['name'])) {
// upload featured image
$config['upload_path'] = './uploads/page';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1080';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$data['page'] = $this->page_model->find($id);
$data['category'] = $this->category_model->all();
$data['error'] = $this->upload->display_errors();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
$data = array('upload_data' => $this->upload->data());
$featured_img = $_FILES['userfile']['name'];
var_dump($data);
var_dump($featured_img);
}
} // else {
if (isset($featured_img)) {
$param['featured_img'] = $featured_img;
}
$param['id'] = $id;
$this->page_model->update($param);
redirect('backend/page', 'refresh');
// }
}
}
型号:
public function update($param) {
$data = array(
'title' => ucwords($this->input->post('title')),
'slug' => strtolower($this->input->post('slug')),
'content' => $this->input->post('content'),
// 'featured_img' => $featured_img,
'category_id' => $this->input->post('category_id')
);
if (isset($param['featured_img'])) {
$data['featured_img'] = $param['featured_img'];
}
$this->db->where('id', $param['id']);
return $this->db->update('pages', $data);
}
我是 codeigniter 的新手。 我正在为这个项目使用 codeigniter。 我不知道如何更新数据库中的图像表单数据。 我已经插入,显示数据库中的数据已完成。 但我不明白如何更新数据库中的图像。 这是我用来更新现有图像的方法,但我无法 更新图像。 我已成功将图片上传到我的数据库中。
public function update($id) {
$this->form_validation->set_rules('tite', 'title', 'required');
$this->form_validation->set_rules('slug', 'Slug', 'required');
if ($this->form_validation->run() === FALSE) {
$data['category'] = $this->category_model->all();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
if (!empty($_FILES['userfile']['name'])) {
// upload featured image
$config['upload_path'] = './uploads/page';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1080';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$data['page'] = $this->page_model->find($id);
$data['category'] = $this->category_model->all();
$data['error'] = $this->upload->display_errors();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
$data = array('upload_data' => $this->upload->data());
$featured_img = $_FILES['userfile']['name'];
var_dump($data);
var_dump($featured_img);
}
} else {
$this->page_model->update($featured_img, $id);
redirect('backend/page', 'refresh');
}
}
}
这是我用来更新现有图像的方法,但我无法更新图像。 这是我发送数据的模型。 我已成功将图像上传到我的数据库中。
public function update($featured_img, $id) {
$data = array(
'title' => ucwords($this->input->post('title')),
'slug' => strtolower($this->input->post('slug')),
'content' => $this->input->post('content'),
'featured_img' => $featured_img,
'category_id' => $this->input->post('category_id')
);
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
试试下面的代码,它会更新 featured_img
仅当它包含数据时(如果用户上传图片)。
控制器:
public function update($id) {
$this->form_validation->set_rules('tite', 'title', 'required');
$this->form_validation->set_rules('slug', 'Slug', 'required');
if ($this->form_validation->run() === FALSE) {
$data['category'] = $this->category_model->all();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
if (!empty($_FILES['userfile']['name'])) {
// upload featured image
$config['upload_path'] = './uploads/page';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1080';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$data['page'] = $this->page_model->find($id);
$data['category'] = $this->category_model->all();
$data['error'] = $this->upload->display_errors();
$this->load->view('backend/templates/header');
$this->load->view('backend/templates/navigation');
$this->load->view('backend/pages/edit', $data);
$this->load->view('backend/templates/footer');
} else {
$data = array('upload_data' => $this->upload->data());
$featured_img = $_FILES['userfile']['name'];
var_dump($data);
var_dump($featured_img);
}
} // else {
if (isset($featured_img)) {
$param['featured_img'] = $featured_img;
}
$param['id'] = $id;
$this->page_model->update($param);
redirect('backend/page', 'refresh');
// }
}
}
型号:
public function update($param) {
$data = array(
'title' => ucwords($this->input->post('title')),
'slug' => strtolower($this->input->post('slug')),
'content' => $this->input->post('content'),
// 'featured_img' => $featured_img,
'category_id' => $this->input->post('category_id')
);
if (isset($param['featured_img'])) {
$data['featured_img'] = $param['featured_img'];
}
$this->db->where('id', $param['id']);
return $this->db->update('pages', $data);
}