2016-04-10 108 views
-1

我正在製作個人資料更新Android應用程序。我需要幫助來獲取JSON值,因爲我得到JSON結果爲空 - 任何人都可以發現錯誤?Android更新MySQL表(Android個人資料)

配置文件更新響應:

{"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; 
    }  

} 
+3

我們不會爲您調試。調試你的代碼,看看有什麼不對。如果您遇到特定代碼行的問題,請返回。 –

+0

我有一個返回值沒有更新的問題 –

+0

我在這裏要做的第一件事就是使用參數綁定 - 這看起來很容易受到SQL注入的影響。 – halfer

回答

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列表

PHP手冊中的所有MySQLi「聲明」方法are documented here,都有很好的例子。請仔細閱讀我使用的每種方法 - 手冊是關於PHP的最好的事情之一!

堆棧溢出還有一個關於SQL注入的set of definitive answers - 那裏有PDO和MySQLi的資源。

一旦你做出了這些改變,我建議逐步檢查你的代碼,一次一行,檢查你得到的中間值是你期望的。

相關問題