3

I am trying to insert images in mysql database with other data but its shows error. its shows the $msg of not saved & repeat data of view file maybe due to $error which i set. PS: I set 'image' datatype varchar in database. here is my view file:

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

this is my controller:

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

& this is my model:

    public function save($data)
  {

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

  }
3
  • "its shows error. its shows the $msg of not saved & repeat data of view file maybe due to $error which i set." don't know what this means. can you share the exact error output you are receiving? Commented Dec 24, 2018 at 0:11
  • Are you using form_open_multipart() or does your form have a "multipart" attribute? That will be needed to work with uploads. Commented Dec 24, 2018 at 0:37
  • $this->upload->data() returns array and you are passing it simply to save in db. The line 'image' => $this->upload->data() should be 'image' => $this->upload->data()['file_name'] Commented Dec 24, 2018 at 11:42

1 Answer 1

1

Your form must have the multipart attribute in HTML file like below :

If you're using form helper, then it should be

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

Else your form should have the enctype attribute like below

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

Then the uploaded data result $this->upload->data() will come in array. So you can't store your array in mysql column. So you need to get the filename from $this->upload->data() and store it in a variable like below.

Your Controller should be

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');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.