图片未使用 codeigniter 插入 mysql

image not insert in mysql using codeigniter

我正在尝试将图像与其他数据一起插入 mysql 数据库,但它显示错误。 它显示未保存的 $msg 和重复视图文件的数据可能是由于我设置的 $error。 PS:我在数据库中设置了 'image' 数据类型 varchar。 这是我的视图文件:

        <input type="file" class="form-control-file" name="image" id="exampleInputFile" >

这是我的控制器:

 public function save()
{
  $this->load->model('Partner_model');
 $feature = $this->input->post('feature');

  $config['upload_path']          = './uploads/files';
           $config['allowed_types']        = 'gif|jpg|png';
           $config['max_size']             = 100;
           $config['max_width']            = 1024;
           $config['max_height']           = 768;
           $this->load->library('upload', $config);
           if ( ! $this->upload->do_upload('image'))
{
       $error = array('error' => $this->upload->display_errors());
       $this->load->view('partner_profile', $error);
}
else
{


  $user_data= array(

   'pname' => $this->input->post('pname'),
   'type' => $this->input->post('type'),
   'address' => $this->input->post('address'),
   'about' => $this->input->post('about'),
   'city' => $this->input->post('city'),
   'code' => $this->input->post('code'),
   'state'=>$this->input->post('state'),
//  'image'=>$this->upload->do_upload('image')
  'feature'=>implode(",",$feature),
   'image' => $this->upload->data()

    );  
   }

    if($this->Partner_model->save($user_data))
   {
       $msg = "save sucesss" ;
   }
   else
   {
       $msg = "not save";
   }

   $this->session->set_flashdata('msg', $msg);
   $this->load->view('partner_profile');
 }
}

&这是我的模型:

    public function save($data)
  {

   return $this->db->insert('property', $data);

  }

您的表单必须在 HTML 文件中具有 multipart 属性,如下所示:

如果您使用的是表单助手,那么它应该是

<?php echo form_open_multipart('/save');?>

否则您的表单应具有如下所示的 enctype 属性

<form enctype="multipart/form-data">

然后上传的数据结果$this->upload->data()会数组进来。所以你不能将数组存储在 mysql 列中。因此,您需要从 $this->upload->data() 获取文件名并将其存储在如下变量中。

你的控制器应该是

public function save(){
 $this->load->model('Partner_model');
 $feature = $this->input->post('feature');

 $config['upload_path']          = './uploads/files';
 $config['allowed_types']        = 'gif|jpg|png';
 $config['max_size']             = 100;
 $config['max_width']            = 1024;
 $config['max_height']           = 768;
 $this->load->library('upload', $config);
 if ( ! $this->upload->do_upload('image')){
   $error = array('error' => $this->upload->display_errors());
   $this->load->view('partner_profile', $error);
 }else{
  $imageArray = $this->upload->data();
  $image =  $imageArray['file_name'];
  $user_data= array(
   'pname' => $this->input->post('pname'),
   'type' => $this->input->post('type'),
   'address' => $this->input->post('address'),
   'about' => $this->input->post('about'),
   'city' => $this->input->post('city'),
   'code' => $this->input->post('code'),
   'state'=>$this->input->post('state'),
   'feature'=>implode(",",$feature),
   'image' => $image
  );  
 }
 if($this->Partner_model->save($user_data)) {
   $msg = "save sucesss" ;
 }else {
   $msg = "not save";
 }
 $this->session->set_flashdata('msg', $msg);
 $this->load->view('partner_profile');
}