2017-03-11 45 views
0

我正在創建一個應用程序,我必須維護一個表(登錄註冊),用於將我的android應用程序連接到我使用PHP的表。無法從PHP獲得適當的答覆android

的Android代碼

 try { 
      JSONObject jsonResponse = new JSONObject(response); 
       boolean success = jsonResponse.getBoolean("success"); 
       if (success) { 
      Intent intent = new Intent(RegisterActivity.this, Login.class); 
      RegisterActivity.this.startActivity(intent); 
     } else if(!success) 
     { JSONObject jsonResponse2 = new JSONObject(response); 
      String errval = new String(); 
     errval=jsonResponse2.getString("errval"); 
     AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this); 
     builder.setMessage(errval) 
     .setNegativeButton("retry", null) 
      .create() 
      .show(); 
    } 
    } catch (JSONException e) { 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), "Error submitting registration", Toast.LENGTH_SHORT).show(); 
        } 


    PHP code:- 
    require 'config.php'; 
    $file = 'outfile.txt'; 
    if ($_POST) { 
file_put_contents($file,"~~~~~~~~~~~~~~~~~~~~\n"); 
foreach ($_POST as $KEY => $VAL) { 
    file_put_contents($file,$KEY."=>".$VAL."\n", FILE_APPEND); 
      } 
if (@$_POST["Email_id"] and @$_POST["hash"]) { 
    $email = strtolower($_POST["Email_id"]); 
    $username = strtolower($_POST["Username"]); 
    $hash = $_POST["hash"]; 
    $echk = $my->Execute("select count(*) from users where email = '$email'"); 
    if ($echk->fields[0] == 0) { 
     $uchk = $my->Execute("select count(*) from users where user = '$username'"); 
     if ($uchk->fields[0] == 0) { 
      $ins = $my->Execute("insert into users values (default,'$username','$email','$hash',default)"); 
      $error["success"] = true; 
      $error["errval"] = "success"; 
      file_put_contents($file,"success", FILE_APPEND); 
     } else { 
      $error["success"] = false; 
      $error["errval"] = "User: $username already exists"; 
     } 
    } else { 
     $error["success"] = false; 
     $error["errval"] = "Email: $email already exists"; 
    } 
    } else { 
    $error["errval"] = "Missing a key: 'Email_id' or 'hash'"; 
    } 
    file_put_contents($file,"~~~~~~~~~~~~~~~~~~~~\n", FILE_APPEND); 
    $json = json_encode($error); 
     print_r($json); 
} 
     ?> 

的細節正在在DB每次進入沒有任何問題,代碼也顯示,如果郵件是在表aldready存在,但如果註冊成功,則應用程序拋出異常提交註冊時出錯。不能找出問題的地方 謝謝。 PS。我是新來的還是學習stackoverflow,原諒如果後期有任何錯誤

+0

有一些問題標記代碼抱歉定向不定。 –

+0

您應該嘗試通過Web瀏覽器向PHP代碼提交請求,以便您可以查看返回的錯誤消息。另外,查看PHP日誌中是否有錯誤。 –

+0

另外,當您從Android Studio進行調試時,'e.printStackTrace()'應該會提供很多線索。 –

回答

0
if (@$echk->fields[0] == 0) { 
     $uchk = $my->Execute("select count(*) from users where user = '$username'"); 
     if (@$uchk->fields[0] == 0) { 
      $ins = $my->Execute("insert into users values (default,'$username','$email','$hash',default)"); 
      $error["success"] = true; 
      $error["errval"] = "success"; 
      file_put_contents($file,"success", FILE_APPEND); 
forgot @ in if condition