title: How to update optional fields if not empty in CodeIgniter?
body: <p>So I have this form in which a user can update his information:
  <a href="https://i.stack.imgur.com/60mJ7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/60mJ7.jpg" alt="enter image description here"></a></p>
  
  <p>The problem comes when some of these inputs are optional. Let's say a user updates his email and password, then the update updates but when a user only edits his email and leave the password inputs blank the password in the database should not be updated....it currently changes the password as well even when they're empty.</p>
  
  <p>Here is my HTML:</p>
  
  <pre><code>&lt;?php echo validation_errors('&lt;p class="alert alert-dismissable alert-danger"&gt;'); ?&gt;       
  &lt;?php echo form_open('users/edit/'.$item-&gt;id); ?&gt;
  &lt;div class="nav-tabs-custom"&gt;
      &lt;ul class="nav nav-tabs"&gt;
          &lt;li class="active"&gt;&lt;a href="#basics" data-toggle="tab" aria-expanded="false"&gt;Basics&lt;/a&gt;&lt;/li&gt;
          &lt;li class=""&gt;&lt;a href="#aboutme" data-toggle="tab" aria-expanded="false"&gt;About Me&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;br&gt;
      &lt;div class="tab-content"&gt;
  &lt;!-- Basics --&gt;
          &lt;div class="tab-pane active" id="basics"&gt;
              &lt;!-- Email --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Email', 'email'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-envelope" aria-hidden="true"&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'email',
                          'id'            =&gt; 'email',
                          'maxlength'     =&gt; '150',
                          'class'         =&gt; 'form-control',
                          'value'         =&gt; $item-&gt;email,
                      );
                  ?&gt;
                  &lt;?php echo form_input($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- Avatar Image --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Avatar Image URL', 'avatar_img'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-id-card-o" aria-hidden="true"&gt;&lt;/i&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'avatar_img',
                          'id'            =&gt; 'avatar_img',
                          'class'         =&gt; 'form-control',
                          'placeholder'   =&gt; '96x96 Pixels',
                          'value'         =&gt; $item-&gt;avatar_img
                      );
                  ?&gt;
                  &lt;?php echo form_input($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- Cover Image --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Cover Img URL', 'cover_img'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-id-card-o" aria-hidden="true"&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'cover_img',
                          'id'            =&gt; 'cover_img',
                          'class'         =&gt; 'form-control',
                          'value'         =&gt; $item-&gt;cover_img
                      );
                  ?&gt;
                  &lt;?php echo form_input($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- Occupation --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Occupation', 'occupation'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-briefcase" aria-hidden="true"&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'occupation',
                          'id'            =&gt; 'occupation',
                          'class'         =&gt; 'form-control',
                          'value'         =&gt; $item-&gt;occupation
                      );
                  ?&gt;
                  &lt;?php echo form_input($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- Website --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Website', 'website'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-link" aria-hidden="true"&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'website',
                          'id'            =&gt; 'website',
                          'class'         =&gt; 'form-control',
                          'value'         =&gt; $item-&gt;website
                      );
                  ?&gt;
                  &lt;?php echo form_input($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- Password --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Password', 'password'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-lock" aria-hidden="true"&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'password',
                          'id'            =&gt; 'password',
                          'class'         =&gt; 'form-control',
                          'value'         =&gt; set_value('password'),
                      );
                  ?&gt;
                  &lt;?php echo form_password($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;!-- Password2 --&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo form_label('Confirm Password', 'password2'); ?&gt;
                  &lt;div class="input-group date"&gt;&lt;div class="input-group-addon"&gt;&lt;i class="fa fa-lock" aria-hidden="true"&gt;&lt;/i&gt;&lt;/div&gt;
                  &lt;?php
                      $data = array(
                          'name'          =&gt; 'password2',
                          'id'            =&gt; 'password2',
                          'class'         =&gt; 'form-control',
                          'value'         =&gt; set_value('password2'),
                      );
                  ?&gt;
                  &lt;?php echo form_password($data); ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php echo form_submit('mysubmit', 'Update User', array('class' =&gt; 'btn btn-primary')); ?&gt;
  &lt;?php echo form_close(); ?&gt;
  </code></pre>
  
  <p>and here is what I have tried:</p>
  
  <pre><code>&lt;?php
              $data = array(
                  'email'      =&gt; $this-&gt;input-&gt;post('email'),
                  'avatar_img' =&gt; $this-&gt;input-&gt;post('avatar_img'),
                  'cover_img' =&gt; $this-&gt;input-&gt;post('cover_img'),
                  'occupation' =&gt; $this-&gt;input-&gt;post('occupation'),
                  'website' =&gt; $this-&gt;input-&gt;post('website'),
                  'password' =&gt; password_hash($this-&gt;input-&gt;post('password'), PASSWORD_DEFAULT),
                  'password2' =&gt; password_hash($this-&gt;input-&gt;post('password2'), PASSWORD_DEFAULT),
              );
  
              if($this-&gt;input-&gt;post('password') != ''){
                  $data['password'] = ($this-&gt;input-&gt;post('password') &amp;&amp; !empty($this-&gt;input-&gt;post('password'))) ? $this-&gt;input-&gt;post('password') : NULL;
              }
              if($this-&gt;input-&gt;post('password2') != ''){
                  $data['password2'] = ($this-&gt;input-&gt;post('password2') &amp;&amp; !empty($this-&gt;input-&gt;post('password2'))) ? $this-&gt;input-&gt;post('password2') : NULL;
              }
  
              // Update User
              $this-&gt;User_model-&gt;update($id, $data);
  ?&gt;
  </code></pre>
  
  <p>but it just don't work, so I tried making more simple:</p>
  
  <pre><code>&lt;?php
      $data = array(
          'email'      =&gt; $this-&gt;input-&gt;post('email'),
          'avatar_img' =&gt; $this-&gt;input-&gt;post('avatar_img'),
          'cover_img' =&gt; $this-&gt;input-&gt;post('cover_img'),
          'occupation' =&gt; $this-&gt;input-&gt;post('occupation'),
          'website' =&gt; $this-&gt;input-&gt;post('website'),
          'password' =&gt; password_hash($this-&gt;input-&gt;post('password'), PASSWORD_DEFAULT),
          'password2' =&gt; password_hash($this-&gt;input-&gt;post('password2'), PASSWORD_DEFAULT),
      );
  
      if($this-&gt;input-&gt;post('password') != ''){
  $data['password2'] = password_hash($this-&gt;input-&gt;post('password2'), PASSWORD_DEFAULT);
      }
      if($this-&gt;input-&gt;post('password2') != ''){
          $data['password2'] = password_hash($this-&gt;input-&gt;post('password2'), PASSWORD_DEFAULT);
      }
  
      // Update User
      $this-&gt;User_model-&gt;update($id, $data);
  ?&gt;
  </code></pre>
  
  <p>Update method in Userr model:</p>
  
  <pre><code>public function update($id, $data)
  {
      $this-&gt;db-&gt;where('id', $id);    
      $this-&gt;db-&gt;update($this-&gt;table, $data);
  }
  </code></pre>
  
  <p>Thanks in advance.</p>