Click here to Skip to main content
14,634,939 members
Rate this:
Please Sign up or sign in to vote.
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(){

private function do_upload(){
    $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);


                    $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->session->set_userdata('message','Product saved successfully');


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

                  'product_image'    => $path_name


    $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">

                    <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 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>

            <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 class="form-actions">
                        <button type="submit" class="btn btn-primary">Save changes</button>
                        <button type="reset" class="btn">Cancel</button>

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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