未使用codeigniter在mysql中插入图像

nvbavucw  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(356)

我试图在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);

  }
j8ag8udp

j8ag8udp1#

您的表单在html文件中必须具有multipart属性,如下所示:
如果您使用的是form helper,那么应该是

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

相关问题