更新codeigniter中的数据而不选择表单中的文件字段

时间:2014-06-25 09:52:52

标签: codeigniter

我正在更新我拥有图像文件的数据库中的数据。所以我想要的是将条件放在图像输入字段上,如果用户想要更新数据以及图像,那么图像将相应地更新如果用户不想更新图像,其余的数据将会更新,因为我在控制器中获取图像输入字段数据时遇到问题。我无法直接在控制器中获取文件名请帮帮我。 这是代码。

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就在这里。

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

2 个答案:

答案 0 :(得分:2)

替换

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

使用

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

最佳做法是使用isset()查看数据是否正在设置

答案 1 :(得分:0)

替换

  

if(!$ this-&gt; upload-&gt; do_upload())

使用

  

if(!$ this-&gt; upload-&gt; do_upload($ _ FILES [&#39; userfile&#39;] [&#39; name&#39;]))