简体   繁体   中英

How can I display a user's balance in the next activity (Android) after login using Volley Library, PHP, and MySQL?

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.

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';
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM