更新记录时面临的问题

时间:2018-07-12 04:49:08

标签: php codeigniter

我正在尝试更新我的记录,并且工作正常,但是在尝试修复该记录时,我遇到了一些问题(但无法这样做)。

我面临的问题是,当我不想更新图像(例如,我只想仅更新我的名字和姓氏)时,只有那些应该更新,而当我尝试更新时这样做,将删除图像名称(存储在我的数据库中),并且将没有可用的图像。

那么有人可以指导我该怎么做吗?

控制器部分:

                    public function updatedata()
                {
                  $id=$this->input->get('id');
                  $result['data']=$this->Form_model->displayrecordsById($id);



                      $this->form_validation->set_rules('fname', 'First name', 'required');
                      $this->form_validation->set_rules('lname', 'Last name', 'required');
                      $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
                      $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

                      if ($this->form_validation->run() == FALSE)
                      {

                          echo validation_errors();
                          $this->load->view('update_records',$result);
                          //exit();
                      } 

                      else 
                      {
                          $config['upload_path']   = './uploads/';
                          $config['allowed_types'] = 'gif|jpg|png';
                          $this->load->library('upload', $config);


                              $fn=$this->input->post('fname');
                              $ln=$this->input->post('lname');
                              $un=$this->input->post('username');
                              $em=$this->input->post('email');
                              if($this->upload->do_upload('filename'))
                                   {
                                       $fi= $this->upload->data('file_name');
                                   }
                              else
                                   {
                                      $fi= $result['data']->filename;
                                   }
                              $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
                              echo 'Successfully updated your record';
                              //exit();
                          } 
        //                   else 
        //                   {
        //                       echo $this->upload->display_errors();
        //                       //exit();
        //                   }
                      }

查看部分:

        <body>
         <?php
          $i=1;
          foreach($data as $row)
          {
          ?>
            <form method="post" enctype="multipart/form-data">
                    <div class="container">

                        <h5 style="color:#ff1a1a">First name</h5> 
                        <input type="text" class="form-control" name="fname" value="<?php echo $row->first_name; ?>"/>
                        <span class="text-danger"><?php echo form_error("fname");?></span>

                       <h5 style="color:#ff1a1a">Last name</h5>
                       <input type="text" class="form-control" name="lname" value="<?php echo $row->last_name; ?>"/>
                       <span class="text-danger"><?php echo form_error("lname");?></span>

                        <h5 style="color:#ff1a1a">Username</h5>
                        <input type="text" class="form-control" name="username" value="<?php echo $row->username; ?>"/>
                        <span class="text-danger"><?php echo form_error("username");?></span>

                        <h5 style="color:#ff1a1a">E-mail</h5>
                        <input type="text" class="form-control" name="email" value="<?php echo $row->email; ?>"/>
                        <span class="text-danger"><?php echo form_error("email");?></span>

                        <h5 style="color:#ff1a1a">Image</h5>
                        <img src="<?php echo base_url('uploads/' . $row->filename);?>" class="img-responsive" alt="image" width="100px" height="100px"><br><input type="file" name="filename" value="<?php echo $row->filename; ?>">

                        <br><input type="submit" name="update" class="btn btn-success" value="Update Records"/>

                    </div>    
            </form>
            <?php } ?>
        </body>

模型零件

        //get values 
    function displayrecordsById($id)
{
        $query=$this->db->query("select * from form where ID='".$id."'");
        return $query->result();
}

    //update record
    function updaterecords($fn,$ln,$un,$em,$fi,$id)
{
        $query=$this->db->query("update form SET first_name='$fn',last_name='$ln',username='$un',email='$em',filename='$fi' where ID='".$id."'");
}

1 个答案:

答案 0 :(得分:0)

请尝试以下给出的代码:

function updaterecords($fn = '', $ln = '',$un = '', $em = '', $fi = '', $id = '')
{
    $data = array();
    if (isset($fn) && !empty($fn))
        $data['first_name'] = $fn;
    if (isset($ln) && !empty($ln))
        $data['last_name'] = $ln;
    if (isset($un) && !empty($un))
        $data['username'] = $un;
    if (isset($em) && !empty($em))
        $data['email'] = $em;
    if (isset($fi) && !empty($fi))
        $data['filename'] = $fi;
    if (!empty($data) && !empty($id))
        $this->db->update('form', $data, array('ID' => $id)); 
}