[英]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.