【问题标题】:Android update MySQL table (Android profile)Android 更新 MySQL 表(Android 配置文件)
【发布时间】:2016-04-10 10:53:00
【问题描述】:

我正在制作个人资料更新 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 注入的影响。
  • 如何更改
  • 我在下面添加了关于安全问题的答案。我还想知道您的if ($no_of_rows > 0) { 子句是否应该return 什么?

标签: php android mysql json


【解决方案1】:

我不知道这是否与您的问题有关,但您不妨先更改可能易受攻击的代码,因为您事先进行的任何错误跟踪都可能需要再次进行。您的代码很可能容易受到 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列表中的每一项

PHP 手册中的所有 MySQLi“语句”方法are documented here,并且都有很好的示例。请仔细阅读我使用的每种方法 - 手册是关于 PHP 最好的东西之一!

Stack Overflow 也有一个关于 SQL 注入的set of definitive answers - PDO 和 MySQLi 都有资源。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2021-08-27
    • 2018-12-26
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多