简体   繁体   English

使用 Volley Library、PHP 和 MySQL 登录后,如何在下一个活动 (Android) 中显示用户的余额?

[英]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.我已经能够让应用程序成功地使用 php 验证来自 sql db 的用户名和密码。 The app uses volley library for the networking.该应用程序使用 volley 库进行网络连接。 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?我知道我必须在 php 脚本中查询数据库然后 encode_json() 但是我如何传递用户名和密码? 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.我正在使用 FILTER_POST[INPUT_POST,""] 将用户的用户名和密码从 loginactivity(android) 传递到 php 脚本,但在 php 脚本中使用 FILTER_POST[],$_SESSION 似乎影响了 JSON 输出并且没有显示任何内容. 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这是 login.php 脚本

<?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 脚本,它将显示用户的余额和贷款

<?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.我曾尝试使用 $_SESSION[] 将用户名从第一个脚本传递到第二个脚本,但它不起作用。

in first php script make changes here在第一个 php 脚本中进行更改

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Volley从登录php获取响应,但未进入下一个活动 - Android Volley getting response from login php but not going to next activity 如何使用php在mysql中特定用户登录后显示数据? - How to display Data after particular user login in mysql using php? 使用PHP,MySQL和Volley进行Android登录 - Android Login Using PHP, MySQL and Volley 成功登录php后如何显示用户名? - How can I display a user's username after successful php login? 如何在我的 android 应用程序中使用全局变量从 mysql 检索用户的登录 ID? - how can I retrieve user's login id from mysql using global variable in my android app? 使用会话登录后显示用户名(php和sqlsrv) - Display user's name after login using session (php and sqlsrv) 使用 PHP 登录后如何显示用户名? - How can i display the User's name after loggin in using PHP? 如何使用基于id的PHP在MySQL中获取列的总和并在Android中使用Volley显示它 - How to fetch sum of column in MySQL using PHP based on id and display it in Android using Volley 如何使用Android中的Volley库将数据保存到mysql中? - How to Save data into mysql using volley library in android? 使用Google+ API(PHP),如何创建公共活动供稿始终显示特定用户的数据,但不显示当前登录的用户? - Using the Google+ API (PHP,) how can I make a public activity feed always display a specific user's data, but not the current logged in user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM