未使用codeigniter在mysql中插入图像

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

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

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

这是我的控制器:

  1. public function save()
  2. {
  3. $this->load->model('Partner_model');
  4. $feature = $this->input->post('feature');
  5. $config['upload_path'] = './uploads/files';
  6. $config['allowed_types'] = 'gif|jpg|png';
  7. $config['max_size'] = 100;
  8. $config['max_width'] = 1024;
  9. $config['max_height'] = 768;
  10. $this->load->library('upload', $config);
  11. if ( ! $this->upload->do_upload('image'))
  12. {
  13. $error = array('error' => $this->upload->display_errors());
  14. $this->load->view('partner_profile', $error);
  15. }
  16. else
  17. {
  18. $user_data= array(
  19. 'pname' => $this->input->post('pname'),
  20. 'type' => $this->input->post('type'),
  21. 'address' => $this->input->post('address'),
  22. 'about' => $this->input->post('about'),
  23. 'city' => $this->input->post('city'),
  24. 'code' => $this->input->post('code'),
  25. 'state'=>$this->input->post('state'),
  26. // 'image'=>$this->upload->do_upload('image')
  27. 'feature'=>implode(",",$feature),
  28. 'image' => $this->upload->data()
  29. );
  30. }
  31. if($this->Partner_model->save($user_data))
  32. {
  33. $msg = "save sucesss" ;
  34. }
  35. else
  36. {
  37. $msg = "not save";
  38. }
  39. $this->session->set_flashdata('msg', $msg);
  40. $this->load->view('partner_profile');
  41. }
  42. }

&这是我的模型:

  1. public function save($data)
  2. {
  3. return $this->db->insert('property', $data);
  4. }
c6ubokkw

c6ubokkw1#

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

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

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

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

然后上传的数据结果$this->upload->data()将以数组形式出现。所以不能将数组存储在mysql列中。因此,您需要从$this->upload->data()获取文件名,并将其存储在如下所示的变量中。
你的控制器应该是

  1. public function save(){
  2. $this->load->model('Partner_model');
  3. $feature = $this->input->post('feature');
  4. $config['upload_path'] = './uploads/files';
  5. $config['allowed_types'] = 'gif|jpg|png';
  6. $config['max_size'] = 100;
  7. $config['max_width'] = 1024;
  8. $config['max_height'] = 768;
  9. $this->load->library('upload', $config);
  10. if ( ! $this->upload->do_upload('image')){
  11. $error = array('error' => $this->upload->display_errors());
  12. $this->load->view('partner_profile', $error);
  13. }else{
  14. $imageArray = $this->upload->data();
  15. $image = $imageArray['file_name'];
  16. $user_data= array(
  17. 'pname' => $this->input->post('pname'),
  18. 'type' => $this->input->post('type'),
  19. 'address' => $this->input->post('address'),
  20. 'about' => $this->input->post('about'),
  21. 'city' => $this->input->post('city'),
  22. 'code' => $this->input->post('code'),
  23. 'state'=>$this->input->post('state'),
  24. 'feature'=>implode(",",$feature),
  25. 'image' => $image
  26. );
  27. }
  28. if($this->Partner_model->save($user_data)) {
  29. $msg = "save sucesss" ;
  30. }else {
  31. $msg = "not save";
  32. }
  33. $this->session->set_flashdata('msg', $msg);
  34. $this->load->view('partner_profile');
  35. }
展开查看全部

相关问题