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


Android update MySQL table (Android profile)

我正在制作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;
    }       
}

我不知道这是否与您的问题有关,但您最好先更改可能存在漏洞的代码,因为在重新部署之前进行的任何错误跟踪都可能需要再次进行。您的代码可能容易受到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手册中进行了说明,并且都有非常好的例子。请仔细阅读我使用过的每一种方法——手册是PHP最好的东西之一!

Stack Overflow也有一组关于SQL注入的明确答案——PDO和MySQLi都有资源。

一旦您做出了这些更改,我建议您一次一行地遍历代码,以检查您获得的中间值是否符合您的期望。