-3

I have been working on an app that allows registered users login to check their balances. I have been able to get the app to validate the username and password from the sql db using php succesfully. The app uses volley library for the networking. Now here is my problem, upon successful login how do I get my app to display the user's balances from the db in the next activity. I know i have to query the db and then encode_json() in the php script but how do I pass the username and password? I'm using FILTER_POST[INPUT_POST,""] to pass the user's username and password from the loginactivity(android) to the php script but using the FILTER_POST[],$_SESSION in php script seems to affect the JSON output and nothing gets displayed. Any help would be very much appreciated.

This is the login method:

public void login(){
    StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.1.139/loginapp/login.php",
            new Response.Listener<String>(){
        @Override
        public void onResponse(String response) {
            if (response.contains("1")){
                startActivity(new Intent(getApplicationContext(),Main2Activity.class));
            }else{
                Toast.makeText(getApplicationContext(),
                        "Wrong username or password", Toast.LENGTH_SHORT).show();
            }

        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("username",et_username.getText().toString());
            params.put("password",et_password.getText().toString());
            return params;
        }
    };
    Volley.newRequestQueue(this).add(request);
}

This is the login.php script

<?php
    define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASS', 'password');
    define('DB_NAME', 'employee101');

    //connecting to database and getting the connection object
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

   //Checking if any error occured while connecting
   if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
   }

   $username = filter_input(INPUT_POST, "username");
   $password = filter_input(INPUT_POST, "password");

   $mysqli = new mysqli("localhost","root","password","employee101");
   $result = mysqli_query($mysqli,"select * from employee_data where 
   username = '".$username."' and password = '".$password."'");
   if ($data = mysqli_fetch_array($result)){
   echo '1';
   }else{
   echo '0';
   }

   ?>

This is the php script for the second activity which will show the user's balance and loan

<?php 

 //database constants
 define('DB_HOST', 'localhost');
 define('DB_USER', 'root');
 define('DB_PASS', 'password');
 define('DB_NAME', 'employee101');

 //connecting to database and getting the connection object
 $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

 //Checking if any error occured while connecting
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 die();
 }

 //creating a query
 $stmt = $conn->prepare("SELECT id, name, surname, age, username, password 
 FROM employee_data where username = '".$username."' and password = 
 '".$password."';");

 //executing the query 
 $stmt->execute();

 //binding results to the query 
 $stmt->bind_result($id, $name, $surname, $age, $username, $password);

 $products = array(); 

 //traversing through all the result 
 while($stmt->fetch()){
 $temp = array();
 $temp['id'] = $id; 
 $temp['name'] = $name; 
 $temp['surname'] = $surname; 
 $temp['age'] = $age; 
 $temp['username'] = $username; 
 $temp['password'] = $password; 
 array_push($products, $temp);
 }

 //displaying the result in json format 
 echo json_encode($products);

I have tried using $_SESSION[] to pass the username from the first script to the second but it doesn't work.

Cross
  • 1
  • 1
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – ADM Apr 04 '19 at 10:21
  • Please add the code for how you are using volley library for making network request – primo Apr 04 '19 at 10:39
  • @primo i have add my code...please help – Cross Apr 04 '19 at 12:42
  • no need to write second script instead after successful login you can fetch id from your table in first script only and then return json array and in volley response you will get the json array from which you will get username – primo Apr 04 '19 at 12:46
  • @primo tried this. Still no display.I noticed that removing $username = filter_input(INPUT_POST, "username"); $password = filter_input(INPUT_POST, "password"); and replacing both variables with the actual string value in db such as $username = "Ben " and $password = "Afleck" worked and displayed. Is there a way filter_input(); is preventing JSON from parsing and encoding? What can i do? – Cross Apr 04 '19 at 15:00

1 Answers1

0

in first php script make changes here

 $result = mysqli_query($mysqli,"select * from employee_data where 
   username = '".$username."' and password = '".$password."'");
   if ($data = mysqli_fetch_array($result)){
   echo '1';
   }else{
   echo '0';
   }

like this

$result = mysqli_query($mysqli,"select * from employee_data where 
       username = '".$username."' and password = '".$password."'");
       if ($data = mysqli_fetch_array($result)){
      //return json array here only.You are already fetching employee records here
       }else{
       echo '0';
       }
primo
  • 1,340
  • 3
  • 12
  • 40
  • I tried this. Still no display.I noticed that removing $username = filter_input(INPUT_POST, "username"); $password = filter_input(INPUT_POST, "password"); and replacing both variables with the actual string value in db such as $username = "Ben " and $password = "Afleck" worked and displayed. Is there a way filter_input(); is preventing JSON from parsing and encoding? What can i do? – Cross Apr 04 '19 at 15:03
  • I think there is no need of using filter_input – primo Apr 05 '19 at 04:23
  • How do I POST the username and password because that's the job of filter_input in the PHP script? – Cross Apr 05 '19 at 08:18
  • for what purpose you are using filter_input? – primo Apr 05 '19 at 08:56
  • Pardon me but i'm very much a newbie in andriod development and coding in general.I'm using FILTER_INPUT(INPUT_POST," ") to get the username and paasword that is entered from the Login() method in android. – Cross Apr 05 '19 at 09:04
  • if you are using for this purpose then no need to do this.You can get the username and password from android without filter_input also – primo Apr 05 '19 at 09:08
  • Go through this link,you will get a clear idea of what you want to do https://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ – primo Apr 05 '19 at 09:11
  • Thanks for the link. The tutorial uses AsyncTask for networking which is totally different from volley library. Please if you can just take your time to explain how I can get the username and password without using FILTER_INPUT I would be really appreciate it. I have been stuck for so long. I dream about the problem in my sleep these days. – Cross Apr 05 '19 at 09:54
  • in the link just go through the php part and keep your android code as it is – primo Apr 05 '19 at 09:59
  • and try again and let me know – primo Apr 05 '19 at 09:59