Welcome Guest, Not a member yet? Register   Sign In
Uploading a file in a form with more data
#1

I'm building a website where I want to upload a product with it's name price and description to the database, and upload an image of the product to a folder. I need the name of the image in the database too.
I've tried to change the name of the image, changing the permissions of the images folder, etc.
When I add a new product it is created in the database but the image name field is empty and the image is not uploaded.
When I print the array with $this->upload->data() it is empty.
Here's my code:
Function submit_product 
Code:
public function submit_product(){
       $this->load->library('form_validation');
       $config['upload_path'] = base_url('images/');
       $config['allowed_types'] = 'jpg|png';
       $config['max_size'] = '2048';
       $config['max_width'] = '2048';
       $config['max_height'] = '2048';
       $this->load->library('upload', $config);
       $this->form_validation->set_rules('name', 'Name', 'required|trim');
       $this->form_validation->set_rules('price', 'Price', 'required|trim');
       $this->form_validation->set_rules('category', 'Category', 'required|trim');
       $this->form_validation->set_rules('description', 'Description', 'trim');
       
       if($this->form_validation->run()){
           $this->load->model('config_model');
           $this->upload->do_upload('userfile');
           $filename = $this->upload->data();
           $this->config_model->add_product($filename);
           $this->home();
       } else {
           $this->admin();
       }
   }
Here is the form
Code:
<?php
               echo form_open_multipart('main/submit_product');
               echo "<p>Name: ";
               echo form_input('name',$this->input->post('name'));
               echo "</p>";
               echo "<p>Price: ";
               echo form_input('price', $this->input->post('price'));
               echo "</p>";
               echo "<p>Category: ";
               echo form_input('category', $this->input->post('category'));
               echo "</p>";
               echo "<p>Description: ";
               echo form_input('description', $this->input->post('description'));
               echo "</p>";
               echo "<p>";
               echo form_upload('userfile');
               echo "</p>";
               echo "<p>";
               echo form_submit('product_submit','Add Product');
               echo "</p>";
               echo "<p>";
               echo form_close();
               echo "</p>";
           ?>
           <br>
And here is the model
Code:
public function add_product($filename){
       print_r ($filename);
       $data = array (
           'name' => $this->input->post('name'),
           'price' => $this->input->post('price'),
           'image' => $filename['file_name'],
           'category' => $this->input->post('category'),
           'description' => $this->input->post('description')
       );
       
       $this->db->insert('products',$data);      
   }
Reply
#2

suggest that you try doing just the upload first - then add the rest of the form
this page has sample code for uploading images http://www.codeigniter.com/user_guide/li...ading.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB