在json中丢失字段时无法更新数据库

时间:2022-11-11 15:28:27

I am making an api that can help user update their info follow the input data. But when in input json have field "password" it will update successfully but when json don't have this field i can not update data in database. This is code i used to update data:

我正在制作一个API,可以帮助用户按照输入数据更新他们的信息。但是当输入json有字段“password”时它会成功更新但是当json没有这个字段时我无法更新数据库中的数据。这是我用来更新数据的代码:

public function updateUserInfo(Request $request){
        $postData = $request->all();
        $data = json_decode($postData['postData'], true);
        if(isset($data['password'])){
            $data['password'] = bcrypt($data['password']);
        }
        $popData = $data['UserId'];
        unset($data['UserId']);
        $updateInfo = array();
        foreach($data as $info){
            if($info != null){
                $updateInfo[] = $info;
            }
        }
        $result =  DB::update(GeneralFunctions::makeUpdateString($data, 'User', ' UserId = '.$popData), $updateInfo);
        if($result != null && $result == true){
            return response()->json(['success'=>true, 'data'=>'Update Successful']);
        }else{
            return response()->json(['success'=>false, 'error'=>'We have encountered an error, try again later!']);
        }
    }

This is the json when everything work fine:

当一切正常时,这是json:

$postData = '{ "UserId" : "1",   "password":"12345", "UserName": "minhkhang",  "Address": "11/200" }'

This is the json which will cause error because it is missing password field:

这是json会导致错误,因为它缺少密码字段:

$postData = '{ "UserId" : "1", "UserName": "minhkhang",  "Address": "11/200" }'

This is code i used to make update string follow input json:

这是我用来使更新字符串跟随输入json的代码:

 public static function makeUpdateString($keyvalarr, $table, $where){
        $stringSQL = 'UPDATE '.$table. ' SET ' ;
        foreach($keyvalarr as $fieldname => $updateval){
            if($updateval != null){
                $stringSQL .= $fieldname.' = ? , ';
            }
        }
        $stringSQL =  substr($stringSQL, 0, -2);
        if($where != null){
            $stringSQL .= 'WHERE '.$where;
        }
        return $stringSQL;
    }

Thank you.

1 个解决方案

#1


2  

The code in question here is:

这里的代码是:

if(isset($data['password'])){
    $data['password'] = bcrypt($data['password']);
}

Check to see what

检查看看是什么

isset($data['password'])

is returning outside the if statement, if it is true for password being present in the JSON and not giving you an issue that sounds like what you'd expect, however check to see if that statement by itself is false without the if statement, make sure it isn't still jumping into that, it seems to be the only place your looking for 'password'

正在if语句之外返回,如果密码出现在JSON中是真的并且没有给你一个听起来像你期望的问题,但是在没有if语句的情况下检查该语句本身是否为假,make确定它还没有跳进去,它似乎是你寻找'密码'的唯一地方

#1


2  

The code in question here is:

这里的代码是:

if(isset($data['password'])){
    $data['password'] = bcrypt($data['password']);
}

Check to see what

检查看看是什么

isset($data['password'])

is returning outside the if statement, if it is true for password being present in the JSON and not giving you an issue that sounds like what you'd expect, however check to see if that statement by itself is false without the if statement, make sure it isn't still jumping into that, it seems to be the only place your looking for 'password'

正在if语句之外返回,如果密码出现在JSON中是真的并且没有给你一个听起来像你期望的问题,但是在没有if语句的情况下检查该语句本身是否为假,make确定它还没有跳进去,它似乎是你寻找'密码'的唯一地方