Tom Granot
Tom Granot

Reputation: 1850

Insert form property and image path to DB in Codeigniter

I'm trying to let a user upload an image and an integer using this form:

<?php 
  echo form_open_multipart('upload/do_upload'); 
  $image_data = array(
    'name'=>'userfile'
  );
  echo "<br>File:  ";
  echo form_upload($image_data);
  echo "<br />";
  $int_data=array(
    'name' =>'int',
    'id' =>'int'
  );
  echo "<br><br>Integer:  ";
  echo form_input($int_data);
?>
<?php echo form_submit('','Upload'); ?>
<?php echo form_close(); ?>

Afterwards, I'm using this do_upload function in order to process the image and upload it to the server:

function do_upload(){
  $config['upload_path']='./uploads';
  $config['allowed_types']='gif|jpeg|png|jpg|bmp';
  $config['max_size']='5000';
  $config['max_width']='2000';
  $config['max_height']='2000';
  $this->load->library('upload',$config);

  if( !$this->upload->do_upload()){
    $error = array('error'=>$this->upload->display_errors());
    $this->load->view('header');
    $this->load->view('upload_form',$error);
    $this->load->view('footer');
  }
  else{
    //RESIZE THE IMAGE 
    $data=array('upload_data'=>$this->upload->data());
    $this->resize($data['upload_data']['full_path'],$data['upload_data']['file_name']);
    $this->insert_post();
    $this->load->view('header');
    $this->load->view('upload_success',$data);
    $this->load->view('footer');
  }
}

Notice the insert_post function in the controller? Here it is:

function insert_post(){
  if($_POST){
    $data=array(
      'int'=> $_POST['int'],
      'image_path'=>$upload_data['full_path'] ,
      'active'=>1
    );
    $this->post->insert_post($data);
    redirect(base_url().'posts/');
  } else {
      $this->load->view('upload_success');
  }
}

The insert_post function in the model looks like this:

function insert_post($data){
  $this->db->insert('posts',$data);
  return $this->db->insert_id();
}

Now the problem - I'm having a difficult time inserting the data to the table. Could you please point me in the direction of the problem?

Upvotes: 0

Views: 699

Answers (1)

Nouphal.M
Nouphal.M

Reputation: 6344

You have to pass a data array to your insert function

$this->insert_post($insertData);

Where $insertData will hold the db field name and its corresponding value of the db table. In your case

$this->insert_post($data['upload_data']);

and access from your function in controller as

function insert_post($upload_data){
...........
}

See more info here

Upvotes: 1

Related Questions