Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to build a project with product information with image. I have tried some code given below, but I can not upload image path in the database. I am a newbie trying to learn code.

What I have tried:

Controller code:-
PHP
class Category extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->model('Category_model');

    }
private function do_upload(){
    $config['upload_path']='./uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 1000;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;
    $this->load->library('upload', $config);
    if (  ! $this->upload->do_upload('product_image')){
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('admin/pages/add_product_form', $error);

    }     

                else
                {
                    $upload_data = $this->upload->data();

                    $this->Category_model->insert_data( $upload_data['upload_path']);

                }



        $data = array('upload_data' => $this->upload->data());

}
function  save_product(){
       $this->do_upload();

       $this->session->set_userdata('message','Product saved successfully');
       $this->add_product();

   }
}

Model code:-
PHP
<pre lang="PHP">function insert_data( $path_name){
    $data = array(

                  'product_image'    => $path_name
                 );

    $data['product_status']=1;

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


}

view code:-
HTML
<form class="form-horizontal" action="<?php echo base_url()?>index.php/Category/save_product" enctype="multipart/form-data" method="post">

                <fieldset>
                    <div class="control-group">
                        <label class="control-label" for="typeahead">Product Name </label>
                        <div class="controls">
                            <input type="text" name="product_name" class="span6 typeahead" id="typeahead"  data-provide="typeahead" data-items="4" >

                        </div>
                    </div>




                                                        <div class="control-group">
                                <label class="control-label" for="selectError3">Product Category</label>
                                <div class="controls">
                                  <select name="product_category" id="selectError3">
                                                                      <option>Select Category</option>
                                                                      <?php foreach($category_info as $category){ ?>
                                                                        <option value="<?php echo $category->category_id ?>"><?php echo $category->category_name; ?></option>


                                                                   <?php } ?>   

                                    <option>Option 2</option>
                                    <option>Option 3</option>
                                    <option>Option 4</option>
                                    <option>Option 5</option>
                                  </select>
                                </div>
                              </div>

            <div class="control-group">
                        <label class="control-label" for="typeahead">Product Image </label>
                        <div class="controls">
            <input name="product_image" type="file" name="fileToUpload" id="fileToUpload">

                        </div>
            </div>
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">Save changes</button>
                        <button type="reset" class="btn">Cancel</button>
                    </div>
                </fieldset>
            </form>   

This is my code given above, but still not getting the image path in database, please help in this regard.Thanks in Advance.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900