Android更新MySQL表(Android配置文件)

时间:2016-04-10 10:53:00

标签: php android mysql json

我正在制作个人资料更新Android应用程序。我需要帮助才能获得JSON值,因为我得到了无效的JSON结果 - 任何人都可以发现错误吗?

个人资料更新回复:

{"tag":"profile_update","error":false,"user":{"fname":null,"lname":null,"email":null,"mobile":null,"class":null,"school":null,"uid":null,"profile_pic":null,"created_at":null}}

我的PHP代码:

public function profileUpdate($fname, $lname, $email, $mobile, $class, $school, $uid, $profile_pic){
    $result = mysqli_query($this->con, "SELECT * FROM users WHERE unique_id = '$uid'") 
                                                    or die(mysqli_error($this->con));       
    $path = "userImages/$uid.png";
    $actual_path = "http://192.168.1.101/cedu/login/$path";
    $no_of_rows = mysqli_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysqli_fetch_array($result);
        $old_email = $result['email'];
        $old_profile_pic = $result['profile_pic'];
        $status = 0;
        $otp = rand(100000, 999999); // otp code

        if ($old_email == $email) {
            if ($old_profile_pic == $profile_pic){
                $result = mysqli_query($this->con, "UPDATE `users` SET `firstname` = '$fname',`lastname` = '$lname', `mobile` = '$mobile',`class` = '$class',`school` = '$school' 
                      WHERE `unique_id` = '$uid'") or die(mysqli_error($this->con));
            } else {
                $result = mysqli_query($this->con, "UPDATE `users` SET `firstname` = '$fname',`lastname` = '$lname', `mobile` = '$mobile',`class` = '$class',`school` = '$school' , `profile_pic` = '$actual_path' 
                      WHERE `unique_id` = '$uid'") or die(mysqli_error($this->con));
                file_put_contents($path, base64_decode($profile_pic));                  
            }

        } else {
            if ($old_profile_pic == $profile_pic){
                $result = mysqli_query($this->con, "UPDATE `users` SET `firstname` = '$fname',`lastname` = '$lname', `email` = '$email', `mobile` = '$mobile',`class` = '$class',`school` = '$school' , `otp` = '$otp', `verified` = '$status'  
                      WHERE `unique_id` = '$uid'") or die(mysqli_error($this->con));
            } else {
                $result = mysqli_query($this->con, "UPDATE `users` SET `firstname` = '$fname',`lastname` = '$lname', `email` = '$email', `mobile` = '$mobile',`class` = '$class',`school` = '$school' , `profile_pic` = '$actual_path', `otp` = '$otp', `verified` = '$status'  
                      WHERE `unique_id` = '$uid'") or die(mysqli_error($this->con));
                file_put_contents($path."user".$uid.".jpg", base64_decode($profile_pic));
            }

        }   

    } else {
        //
        return false;
    }       

}

1 个答案:

答案 0 :(得分:0)

我不知道这是否与您的问题有关,但您最好还是先改变您的潜在易受攻击的代码,因为任何跟踪您的错误都可能需要再次进行。您的代码可能容易受到SQL注入的影响。我将在下面添加一个(未经测试的)示例,您将需要:

  • 了解它
  • 在您的应用程序的其余部分进行类似的更改

这是一个可能容易受到攻击的声明:您将用户输入看起来直接注入SQL字符串:

$result = mysqli_query(
    $this->con,
    "SELECT * FROM users WHERE unique_id = '$uid'"
) or die(mysqli_error($this->con));

首先,让我们将其更改为使用显式列名,并绑定:

$statement = mysqli_prepare(
     $this->con, 
    "SELECT email, profile_pic FROM users WHERE unique_id = ?"
) or die(mysqli_error($this->con));
mysqli_stmt_bind_param($statement, "i", $uid);
mysqli_stmt_execute($statement);
mysqli_stmt_bind_result($statement, $email, $profile_pic);

这里发生了什么?

  • 我们使用i类型绑定输入变量,该类型指定它是一个整数
  • 我们使用mysqli_stmt_execute方法
  • 运行查询
  • 我们绑定一个输出变量列表,对应于SELECT列表
  • 中的每个项目

所有MySQLi"陈述" PHP手册中的方法are documented here,都有很好的例子。请阅读我使用的每种方法 - 手册是关于PHP的最佳方法之一!

Stack Overflow在SQL注入上也有set of definitive answers - PDO和MySQLi都有资源。

完成这些更改后,我建议您逐步执行一行代码,检查您获得的中间值是否符合预期。