dongluo3962 2017-03-11 20:00
浏览 45
已采纳

无法从PHP获得正确的回复到android

I am creating an app for which I have to maintain a table(login registration), for connecting my android app to the table I am using PHP.

Android code

     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,"~~~~~~~~~~~~~~~~~~~~
");
foreach ($_POST as $KEY => $VAL) {
    file_put_contents($file,$KEY."=>".$VAL."
", 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,"~~~~~~~~~~~~~~~~~~~~
", FILE_APPEND);
  $json = json_encode($error);
      print_r($json);
 }
       ?>

The details are being entered in the DB without any issues every time, the code also shows if the email is aldready existing in the table, but if the registration is a success, the app throws exception Error submitting registration. cant figure out where the problem is thank you. PS. I am new to stackoverflow still learning, forgive if there are any mistakes in post

  • 写回答

1条回答 默认 最新

  • dongxuandong2045 2017-03-15 14:44
    关注
    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用