Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果CodeIgniter中不为空,如何更新可选字段?_Php_Codeigniter_Passwords_Codeigniter 3_Change Password - Fatal编程技术网

Php 如果CodeIgniter中不为空,如何更新可选字段?

Php 如果CodeIgniter中不为空,如何更新可选字段?,php,codeigniter,passwords,codeigniter-3,change-password,Php,Codeigniter,Passwords,Codeigniter 3,Change Password,因此,我有一个表单,用户可以在其中更新其信息: 当其中一些输入是可选的时,问题就出现了。假设用户更新其电子邮件和密码,然后更新,但当用户仅编辑其电子邮件并将密码输入留空时,数据库中的密码不应更新……即使密码为空,当前也会更改密码 这是我的HTML: <?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?> <?php echo form_o

因此,我有一个表单,用户可以在其中更新其信息:

当其中一些输入是可选的时,问题就出现了。假设用户更新其电子邮件和密码,然后更新,但当用户仅编辑其电子邮件并将密码输入留空时,数据库中的密码不应更新……即使密码为空,当前也会更改密码

这是我的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(); ?>
提前感谢。

如果您希望仅在密码字段不为空时更新数据库中的密码,请按以下方式操作:

<?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')
    );

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

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

我从第一个数组列表中删除了“password”和“password2”,并保持状态检查。我添加了修剪,以防字段中有空格

我希望这能奏效

编辑此代码

$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),
            );


您应该从数据库中获取旧数据,然后使用三元运算符更新值!显示$this->User中的代码_model@runningmark是的,我可以很容易地做到这一点,但它会一次又一次地用相同的值更新它们。你不这样认为吗?@slon检查我更新后的问题。然后使用if-else语句,分配一个数组,最后更新更新后的值数组!妈的,我整个上午都在想办法,而你却一如我所愿。谢谢,伙计!我以前试过,但没用。有人已经帮过我了,但是谢谢你的帮助。
public function update($id, $data)
{
    $this->db->where('id', $id);    
    $this->db->update($this->table, $data);
}
<?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')
    );

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

        // Update User
        $this->User_model->update($id, $data);
?>
$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(!empty($this->input->post('email'))){
       $data['email'] = $this->input->post('email');
    }
    if(!empty($this->input->post('avatar_img'))){
       $data['avatar_img'] = $this->input->post('avatar_img');
    }
    if(!empty($this->input->post('cover_img'))){
       $data['cover_img'] = $this->input->post('cover_img');
    }
    if(!empty($this->input->post('occupation'))){
       $data['occupation'] => $this->input->post('occupation');
    }
    if(!empty($this->input->post('website'))){
       $data['website'] = $this->input->post('website');
    }
    if(!empty($this->input->post('password'))){
       $data['password'] = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
    }
    if(!empty($this->input->post('password2'))){
       $data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
    }