PHP跳过功能变量,当它不存在时

时间:2016-01-19 04:14:22

标签: 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 data不存在,则不要更新表格中的现有数据。因为当我试图在函数上使用NULL作为变量时,我的数据被替换为空数据。

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

谢谢

3 个答案:

答案 0 :(得分:3)

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

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('Required fields missing.');
    }

    $this->update('user', $data, '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比较您的代码,此示例和验证失败。

答案 1 :(得分:3)

如果您无法更改方法签名以取代数组,则可以使用isset()empty()自然地检查每个值,具体取决于您的数据需求:

if (!empty($gender) {
    $data['gender'] => $this->escapeString(escapeString);
}

然而这很乏味。更好的方法是将数组作为输入,然后使用有效和/或必需键的数组。像这样:

class MyClass
{
    private $valid_keys = [
        "first_name", "last_name", "email", "phone", "password", 
        "bank_name", "bank_account_number", "bank_account_name", 
        "gender", "birthday", "address", "area", "default_type"
    ];

    public function changeProfile($profile)
    {
        // Return valid keys
        $profile = array_filter($profile, function ($key) {
            return in_array($key, self::$valid_keys);
        } , ARRAY_FILTER_USE_KEY);

        // Escape values
        $profile = array_map(function ($value) {
            // .. your escape function
        }, $profile);

        // Do your stuff
        $this->update('user', $profile, 'email = "'. $profile['email'].'" AND phone = "'.$profile['phone'].'"');
        $res = $this->getResult();      
    }
}

这样你只能设置有效的数据,并且你有办法一般地逃避它。

哦,是的,当然只有$ profile中的值才会发送到你的更新功能。

如果您无法更改方法签名,可以使用func_get_args()

进行查看
public function changeProfile($first_name, $last_name, $email, $phone, $password, 
                         $bank_name, $bank_account_number, $bank_account_name, 
                         $gender, $birthday, $address, $area, $default_type)
{
    $args = func_get_args();

    $profile = [];
    // Important: $valid_keys should be the same as your function parameters and in the same order for this to work. If you want to go further you can look at `ReflectionMethod` to get the names through reflection.
    foreach ($args as $index => $value) {
        $key = self::$valid_keys[$index];
        $profile[$key] = $this->escapeValue($value);
    }

    // Do your stuff
    $this->update('user', $profile, 'email = "'. $profile['email'].'" AND phone = "'.$profile['phone'].'"');
    $res = $this->getResult();
}

答案 2 :(得分:1)

检查它是否为null然后将其保存在数组中,如下所示

function changeProfile ($first_name, $last_name,$email,$phone) {
   $this->connect();
 $data=array();
 if($first_name!=""){
     $data['first_name']= $this->escapeString($first_name);
 }
  if($last_name!=""){
     $data['last_name']=$this->escapeString($last_name);
 }
if($email!=""){
     $data['email']=$this->escapeString($email);
 }
if($phone!=""){
     $data['phone']=$this->escapeString($phone);
 }
    $this->update('user', $data, 'email = "'.$email.'" AND phone = "'.$phone.'"');
    $res = $this->getResult();


}