当PHP不存在时,PHP跳过函数变量

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了当PHP不存在时,PHP跳过函数变量脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个代码

public function changePRofile ($First_name,$last_name,$email,$phone,$password,$bank_name,$bank_account_number,$bank_account_name,$gender,$birthday,$address,$area,$default_tyPE) {

    $this->connect();

    $data = array ('first_name' => $this->escapeString($first_name),'last_name' => $this->escapeString($last_name),'email' => $this->escapeString($email),'phone' => $this->escapeString($phone),'password' => $this->escapeString($password),'bank_name' => $this->escapeString($bank_name),'bank_account_number' => $this->escapeString($bank_account_number),'bank_account_name' => $this->escapeString($bank_account_name),'gender' => $this->escapeString($gender),'birthday' => $this->escapeString($birthday),'address' => $this->escapeString($address),'area' => $this->escapeString($area),'default_type' => $this->escapeString($default_type));

    $this->update('user',$data,'email = "'.$email.'" AND phone = "'.$phone.'"');
    $res = $this->getResult();      

}

现在,我有这样的问题:

我想’跳过’函数上的一些变量,例如$birthday和$gender,这样就不会在sql UPDATE上处理它并让当前数据保持原样.

我的意思是,如果$birthday和$gender数据不存在,则不要更新表中的现有数据.因为当我尝试在函数上使用NULL作为变量时,我的数据被替换为空数据.

如何管理这种情况没有多个if检查每个变量?

谢谢

解决方法

如果你有选项,你应该一个参数数组替换你的函数的参数列表,例如:

public function changeProfile($VARiables)
{
    $this->connect();

    $data = array();
    foreach ($variables as $key => $value) {
        $data[$key] = $this->escapeString($value);
    }

    // Validation?
    if (!isset($data['email'],$data['phone'])) {
        die('@L_777_9@ fields missing.');
    }

    $this->update('user','email = "' . $data['email'] . '" AND phone = "' . $data['phone'] . '"');
    $res = $this->getResult();
}

这假设SQL查询是静态的.如果需要,您还可以为其添加更多字段.

然后你会这样称呼它:

$test->changeProfileUpdated([
    'first_name' => 'john','last_name' => 'doe','email' => 'example@example.COM','phone' => 12345,'password' => 'password1','bank_name' => 'ANZ','bank_account_number' => '123-456','bank_account_name' => 'J DOE','gender' => 'M','birthday' => '1/2/13','address' => '12 Fake St','area' => 'Area 51','default_type' => 'Some default'
]);

Here’s a demo比较您的代码,此示例和验证失败.

脚本宝典总结

以上是脚本宝典为你收集整理的当PHP不存在时,PHP跳过函数变量全部内容,希望文章能够帮你解决当PHP不存在时,PHP跳过函数变量所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。