Click here to Skip to main content
14,634,917 members

Image path not uploading into database PHP codeigniter

Justice taylor asked:

Open original thread
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:-
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:-
<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:-
<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.
Tags: PHP, File-Upload, CodeIgniter

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100