title: JSON Android login validation via mysql
body: <p>I am making a login screen and when testing the code,it always validates both wrong and right inputs and enters the home screen.The PHP works as it can tell which data are found in the database.Heres the code:</p>
  
  <p>loginActivity.java</p>
  
  <pre><code>import android.app.Dialog;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.AsyncTask;
  import android.provider.CalendarContract;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import com.android.volley.AuthFailureError;
  import com.android.volley.Request;
  import com.android.volley.RequestQueue;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.toolbox.JsonObjectRequest;
  import com.android.volley.toolbox.StringRequest;
  import com.android.volley.toolbox.Volley;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class loginDosen extends AppCompatActivity {
      EditText txKodeDosen,txPassword;
      String KodeDosen,password;
      RequestQueue requestQueue;
      String loginURL ="http://192.168.43.217/test/DosenPublikasi/loginDosen.php";
      StringRequest request;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login_dosen);
  
      txKodeDosen = (EditText)findViewById(R.id.txKodeDosen);
      txPassword = (EditText)findViewById(R.id.txPassword);
      requestQueue = Volley.newRequestQueue(loginDosen.this);
  }
  
  public void Login(View view) {
      if(txKodeDosen.getText().toString().equals("") ){
          Toast.makeText(this,"Kode Dosen Field is empty",Toast.LENGTH_SHORT).show();
      }
      else if(txKodeDosen.getText().toString().charAt(0)!='D'){
          Toast.makeText(this,"Must have D in the start",Toast.LENGTH_SHORT).show();
      }else if(txKodeDosen.length() !=5 ){
          Toast.makeText(this,"Must be 5 characters long",Toast.LENGTH_SHORT).show();
      }
      else if(txPassword.getText().toString().equals("")){
          Toast.makeText(this,"Password Field is empty",Toast.LENGTH_SHORT).show();
      }
      else {
          JSONObject Login = new JSONObject();
          try {
              Login.put("kodeDosen", txKodeDosen.getText().toString());
              Login.put("password", txPassword.getText().toString());
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          JsonObjectRequest jsonobjectrequest = new JsonObjectRequest(Request.Method.POST, loginURL, Login,
                  new Response.Listener&lt;JSONObject&gt;()
  
                  {
                      @Override
                      public void onResponse(JSONObject response) {
                          Toast.makeText(loginDosen.this, response.toString(), Toast.LENGTH_SHORT).show();
                          SharedPreferences DataDosen = getSharedPreferences("Dosen", Context.MODE_PRIVATE);
                          SharedPreferences.Editor editor = DataDosen.edit();
                          editor.putString("kodeDosen", txKodeDosen.getText().toString());
                          editor.putString("password", txPassword.getText().toString());
                          editor.commit();
                          Intent intent = new Intent(loginDosen.this, homepageDosen.class);
                          startActivity(intent);
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          Toast.makeText(loginDosen.this, error.toString(), Toast.LENGTH_LONG).show();
                      }
  
                  });
          requestQueue.add(jsonobjectrequest);
      }
  
  
  }
  
  
  
  public void Back(View view) {
      Intent intent = new Intent(loginDosen.this, registrasiDosen.class);
      startActivity(intent);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>heres the php</p>
  
  <pre><code>&lt;?php
  include 'connectdb.php';
  
  $data = json_decode(file_get_contents('php://input'), true);
  //print_r($data);
  
  $kodeDosen =$data["kodeDosen"];
  $password = $data["password"];
  
  $message = array("message"=&gt;"Data found");
  $failure = array("mesage"=&gt;"Data not found");
  
  $sql = "SELECT * FROM tbl_dosen WHERE kodeDosen ='$kodeDosen' and password = '$password'";
  
  //echo $kodeDosen;
  
  $result = mysqli_query($conn,$sql);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
  
  $count = mysqli_num_rows($result);
  
  
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  
  if($count == 1) {
      echo json_encode($message);
  }else {
      echo json_encode($failure);
  }
  ?&gt;
  </code></pre>
  
  <p>the json works but when the login button is used,it will always enter the next screen even if the input doesnt match any data from the database</p>