So I have this form in which a user can update his information:
[![enter image description here](https://i.stack.imgur.com/60mJ7.jpg)](https://i.stack.imgur.com/60mJ7.jpg)

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.

Here is my HTML:

```
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>       
<?php echo form_open('users/edit/'.$item->id); ?>
<div class="nav-tabs-custom">
    <ul class="nav nav-tabs">
        <li class="active"><a href="#basics" data-toggle="tab" aria-expanded="false">Basics</a></li>
        <li class=""><a href="#aboutme" data-toggle="tab" aria-expanded="false">About Me</a></li>
    </ul>
    <br>
    <div class="tab-content">
<!-- Basics -->
        <div class="tab-pane active" id="basics">
            <!-- Email -->
            <div class="form-group">
                <?php echo form_label('Email', 'email'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-envelope" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'email',
                        'id'            => 'email',
                        'maxlength'     => '150',
                        'class'         => 'form-control',
                        'value'         => $item->email,
                    );
                ?>
                <?php echo form_input($data); ?>
                </div>
            </div>
            <!-- Avatar Image -->
            <div class="form-group">
                <?php echo form_label('Avatar Image URL', 'avatar_img'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></i></div>
                <?php
                    $data = array(
                        'name'          => 'avatar_img',
                        'id'            => 'avatar_img',
                        'class'         => 'form-control',
                        'placeholder'   => '96x96 Pixels',
                        'value'         => $item->avatar_img
                    );
                ?>
                <?php echo form_input($data); ?>
                </div>
            </div>
            <!-- Cover Image -->
            <div class="form-group">
                <?php echo form_label('Cover Img URL', 'cover_img'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'cover_img',
                        'id'            => 'cover_img',
                        'class'         => 'form-control',
                        'value'         => $item->cover_img
                    );
                ?>
                <?php echo form_input($data); ?>
                </div>
            </div>
            <!-- Occupation -->
            <div class="form-group">
                <?php echo form_label('Occupation', 'occupation'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-briefcase" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'occupation',
                        'id'            => 'occupation',
                        'class'         => 'form-control',
                        'value'         => $item->occupation
                    );
                ?>
                <?php echo form_input($data); ?>
                </div>
            </div>
            <!-- Website -->
            <div class="form-group">
                <?php echo form_label('Website', 'website'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-link" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'website',
                        'id'            => 'website',
                        'class'         => 'form-control',
                        'value'         => $item->website
                    );
                ?>
                <?php echo form_input($data); ?>
                </div>
            </div>
            <!-- Password -->
            <div class="form-group">
                <?php echo form_label('Password', 'password'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'password',
                        'id'            => 'password',
                        'class'         => 'form-control',
                        'value'         => set_value('password'),
                    );
                ?>
                <?php echo form_password($data); ?>
                </div>
            </div>
            <!-- Password2 -->
            <div class="form-group">
                <?php echo form_label('Confirm Password', 'password2'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'password2',
                        'id'            => 'password2',
                        'class'         => 'form-control',
                        'value'         => set_value('password2'),
                    );
                ?>
                <?php echo form_password($data); ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php echo form_submit('mysubmit', 'Update User', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>

```

and here is what I have tried:

```
<?php
            $data = array(
                'email'      => $this->input->post('email'),
                'avatar_img' => $this->input->post('avatar_img'),
                'cover_img' => $this->input->post('cover_img'),
                'occupation' => $this->input->post('occupation'),
                'website' => $this->input->post('website'),
                'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
                'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
            );

            if($this->input->post('password') != ''){
                $data['password'] = ($this->input->post('password') && !empty($this->input->post('password'))) ? $this->input->post('password') : NULL;
            }
            if($this->input->post('password2') != ''){
                $data['password2'] = ($this->input->post('password2') && !empty($this->input->post('password2'))) ? $this->input->post('password2') : NULL;
            }

            // Update User
            $this->User_model->update($id, $data);
?>

```

but it just don't work, so I tried making more simple:

```
<?php
    $data = array(
        'email'      => $this->input->post('email'),
        'avatar_img' => $this->input->post('avatar_img'),
        'cover_img' => $this->input->post('cover_img'),
        'occupation' => $this->input->post('occupation'),
        'website' => $this->input->post('website'),
        'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
        'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
    );

    if($this->input->post('password') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
    }
    if($this->input->post('password2') != ''){
        $data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
    }

    // Update User
    $this->User_model->update($id, $data);
?>

```

Update method in Userr model:

```
public function update($id, $data)
{
    $this->db->where('id', $id);    
    $this->db->update($this->table, $data);
}

```

Thanks in advance.