Vicky
Vicky

Reputation: 982

updating data in codiegniter with out selecting the file field in form

I am updating the data in database where I have images file too .So what i want is to put the condition on the image input field if the user want to update data as well as the image so the image will be updated accordingly and if user don't want to update image the rest of the data will be updated now I am having problem in getting the image input field data in controller. I am unable to get the filename directly in controller please help me out. Here is the code.

function save_update()
    {
         $id=$this->input->post('id');

        if(!empty($_FILES['userfile']['name']))
        {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $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('upload_successful', $error);
        }
        else
        {

                $data1 = $this->upload->data();
                $filename=$data1['file_name'];

        $data=array(
        'pname'=>$this->input->post('product_name'),
        'pprice'=>$this->input->post('product_price'),
        'pquantity'=>$this->input->post('product_quantity'),
        'pcategory'=>$this->input->post('category'),
        'product_pic'=>$filename
        );
      $result=$this->cartmodel->update_data($data,$id,'product');
        }
      }

     else
     {
        $data=array(
        'pname'=>$this->input->post('product_name'),
        'pprice'=>$this->input->post('product_price'),
        'pquantity'=>$this->input->post('product_quantity'),
        'pcategory'=>$this->input->post('category'),
        'product_pic'=>$this->input->post('oldfile')
        );
      $result=$this->cartmodel->update_data($data,$id,'product');
      if($result==true)
      {
          redirect('cart/admin');
      }
      else
      {
          echo '<script type="text/javascript">alert("sorry Could\'nt delete the file")</script>';
      }

     }

    }

Html is here.

<form class="form-horizontal" method="post" action="<?php echo base_url(); ?>cart/save_update" enctype="multipart/form-data">
                    <?php 
                    if(isset($specific))
                    {?> 
                                  <fieldset>
                                    <legend>Form Components</legend>
                                     <div class="control-group">
                                      <label class="control-label" for="project_tittle">Product Name</label>
                                      <div class="controls">
                                        <input type="text" class="span6" id="typeahead"  name="product_name" value=
                                        "<?=$specific->pname;   ?>">
                                      </div>
                                    </div>
                                    <input type="hidden" name="id" value="<?=$specific->pid ?>">
                                   <div class="control-group">
                                      <label class="control-label" for="project_name">Price </label>
                                      <div class="controls">
                                        <input type="text" class="span6" id="typeahead"  name="product_price" 
                                        value=
                                        "<?=$specific->pprice;   ?>">
                                      </div>
                                    </div>
                                    <div class="control-group">
                                      <label class="control-label" for="project_caption">Quantity </label>
                                      <div class="controls">
                                        <input type="number" class="span6" id="typeahead"  name="product_quantity"
                                        value=
                                        "<?=$specific->pquantity;   ?>">
                                      </div>
                                    </div>
                                    <div class="control-group">
                                      <label class="control-label" for="project_link">Category </label>
                                      <div class="controls">
                                        <select name="category">
                                        <option value=
                                        "<?=$specific->pcategory;   ?>"><?=$specific->pcategory;   ?></option>

                                        </select>
                                      </div>
                                    </div>
                                    <div class="control-group">
                                      <label class="control-label" for="userfile">Product Pic </label>
                                      <div class="controls">
                                        <input type="file" class="span6" id="typeahead"  name="userfile" >
                                      </div>
                                    </div>
                                   <input type="hidden" name="oldfile" value="<?=$specific->product_pic;   ?>" >
                                    <div class="form-actions">
                                      <button type="submit" class="btn btn-primary">Save changes</button>
                                      <button type="reset" class="btn">Cancel</button>
                                    </div>


                                  </fieldset>
                                         <?php
                }
                    ?>
                                </form>

Upvotes: 1

Views: 70

Answers (2)

Gaurav
Gaurav

Reputation: 713

Replace

if ( ! $this->upload->do_upload())

With

if ( ! $this->upload->do_upload($_FILES['userfile']['name']))

Upvotes: 0

Pooshonk
Pooshonk

Reputation: 1324

Replace

if(!empty($_FILES['userfile']['name']))

With

if(isset($_FILES['userfile']))

It is best practice to use isset() to see whether the data is being set or not

Upvotes: 2

Related Questions