2012-12-06 141 views
1

我無法在我的.php腳本中發現錯誤。使用JSON解析器時出現錯誤代碼錯誤

我使用安卓應用發送帖子請求,以註冊包括唯一設備ID和電子郵件的用戶。

發送數據時,我的api會檢查電子郵件或設備ID是否已經存在,因此您無法註冊兩次。

登記時的第一次,一切正常。當我嘗試再次使用相同的電子郵件註冊時,它也可以正常工作(獲得正確的錯誤)。但是,如果我使用不同的電子郵件(但相同的設備ID),我會得到一個錯誤的錯誤代碼。

這裏是PHP-代碼:

else if ($tag == 'register') { 
    // Request type is Register new user 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $password = $_POST['password']; 
    $devid = $_POST['devid']; 

    // check if user already exists 
    if ($db->CheckUser($email)) { 
     // user already exists 
     $response["error"] = 2; 
     $response["error_msg"] = "User already exists"; 
     echo json_encode($response);} 


    else if ($db->CheckDevice($devid)) { 
      // Device already exists 
      $response["error"] = 3; 
      $response["error_msg"] = "Device already exists"; 
      echo json_encode($response);} 
    else { 
     // store user 
     $user = $db->storeUser($name, $email, $password, $devid); 
     if ($user) { 
      // user stored successfully 
      $response["success"] = 1; 
      $response["uid"] = $user["unique_id"]; 
      $response["user"]["name"] = $user["name"]; 
      $response["user"]["email"] = $user["email"]; 
      $response["user"]["devid"] = $user["devid"]; 
      $response["user"]["created_at"] = $user["created_at"]; 
      $response["user"]["updated_at"] = $user["updated_at"]; 
      echo json_encode($response);} 
     else { 
      // user failed to store 
      $response["error"] = 1; 
      $response["error_msg"] = "Error occured in Registration"; 
      echo json_encode($response);} 
    } 
} 

校驗功能:

public function CheckUser($email) { 
    $result = mysql_query("SELECT email from users WHERE email = '$email'"); 
    $no_of_rows = mysql_num_rows($result); 
    if ($no_of_rows > 0) { 
     // user existed 
     return true;} 
    else { 
     // user not existed 
     return false;} 
} 


public function CheckDevice($devid) { 
    $result = mysql_query("SELECT devid from users WHERE devid = '$devid'"); 
    $no_of_rows = mysql_num_rows($result); 
    if ($no_of_rows > 0) { 
     // user existed 
     return true; 
    } else { 
     // user not existed 
     return false; 
    } 
} 

存儲功能:

public function storeUser($name, $email, $password, $devid) { 
    $uuid = uniqid('', true); 
    $hash = $this->hashSSHA($password); 
    $encrypted_password = $hash["encrypted"]; // encrypted password 
    $salt = $hash["salt"]; // salt 
    $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, devid, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', '$devid', NOW())"); 
    // check for successful store 
    if ($result) { 
     // get user details 
     $uid = mysql_insert_id(); // last inserted id 
     $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
     // return user details 
     return mysql_fetch_array($result); 
    } else { 
     return false; 
    } 
} 

我送

我應該得到這個錯誤:

$response["error"] = 3; 
     $response["error_msg"] = "Device already exists"; 

但我得到這個:

$response["error"] = 1; 
      $response["error_msg"] = "Error occured in Registration"; 

不知何故調用CheckDevice功能時,它好像又回到false雖然當我手動使用SELECT devid from users WHERE devid = '$devid'在phpMyAdmin我得到一個true

然後,他無法存儲,因爲設備ID必須是唯一的,並給我錯誤(這是唯一合理的解釋)。

+3

您是SQL注入非常脆弱。你應該明確解決這個問題。 –

+0

這是很多代碼張貼在那裏,沒有真正看到你的數據庫很難看到哪裏喲真的有問題。你有沒有嘗試過調試?在代碼執行路徑中的關鍵位置拋出一些'var_dump()'調用,看看你是否真的得到了你所期望的。這將幫助您快速縮小問題範圍。 –

+0

我知道,但那不是我現在的問題。一旦我完成了基本腳本,我會處理這個問題。這段代碼遠沒有發佈:) – Lotzki

回答

0

你應該在你的CheckDevice方法中做一些echo $devid來查看你的android設備是否真的發送相同的id。

也許你在某處遇到了錯誤(也許你在嘗試從相同設備ID註冊爲第二個用戶時沒有發送任何內容)。

我認爲這是你的問題(你的android應用程序)。

+0

謝謝,我發現錯誤...應該有過這個想法! – Lotzki

0

我發現我的錯誤!

設備ID是15個字符長,表長度爲13

感謝你的幫助,我現在覺得自己像個白癡:)

+0

我覺得所有的時間都是這樣的:) np – pregmatch