我尝试上传图像并将其插入数据库,但无法正常工作。 页面上没有显示任何错误,图像也没有上传或插入数据库, 请给我一个解决方案,该如何解决这个问题?
我的代码在这里,
控制器的代码在这里,enter code here
public function add_product()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$input_name = "product_image";
if (!$this->upload->do_upload($input_name))
{
$data['items'] = $this->data_model->get_data_info('product_id','product');
$this->load->view('product',$data);
}
else
{
$image_info = $this->upload->data();
$data = array(
'product_name'=>$this->input->post('product'),
'product_pic'=>$image_info['file_name'],
'description'=>$this->input->post('description')
);
$query = $this->data_model->add_data($data,'product');
if($query == TRUE){
echo "product added";
$data['items'] = $this->data_model->get_data_info('product_id','product');
$this->load->view('product',$data);
}else{
echo"product already exists";
$data['items'] = $this->data_model->get_data_info('product_id','category');
$this->load->view('product',$data);
}
}
}
型号:
public function add_data($data,$table){
$result = $this->db->get_where($table,$data);
$query = (bool)$result->num_rows();
if(!$query){
if($this->db->insert($table,$data)){
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
答案 0 :(得分:0)
控制器代码
function add_product() {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($_FILES['product_image'])) {
$config['upload_path'] = './Uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $_FILES['product_image']['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('product_image')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload('product_image')) {
$image_data = $this->upload->data();
$full_path = $config['file_name'];
$data["product_pic"] = $full_path;
$query = $this->data_model->add_data($data, 'product');
if ($res == TRUE) {
echo "product added";
$data['items'] = $this->data_model->get_data_info('product_id', 'product');
$this->load->view('product', $data);
} else {
echo"product already exists";
$data['items'] = $this->data_model->get_data_info('product_id', 'category');
$this->load->view('product', $data);
}
}
}
}
$this->load->view('product');
}
型号代码
function add_data($data, $table) {
if ($query = $this->db->insert($table,$data)) {
$insert_id = $this->db->insert_id();
return $insert_id;
} else {
return false;
}
}