我连接到一个对其有查询的PHP文件。现在我需要知道如何检索de user ID Session并将其传递给PHP进行查询,并且只显示登录用户的表数据。
Config.java(我开始做共享首选项的地方)
//Keys for email and password as defined in our $_POST['key'] in login.php
public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";
public static final String KEY_ID = "id_utilizador";
//If server response is equal to this that means login is successful
public static final String LOGIN_SUCCESS = "success";
//Keys for Sharedpreferences
//This would be the name of our shared preferences
public static final String SHARED_PREF_NAME = "myloginapp";
//This would be used to store the email of current logged in user
public static final String EMAIL_SHARED_PREF = "email";
//This would be used to store the id of current logged in user
public static final String ID_SHARED_PREF = "id_utilizador";
//We will use this to store the boolean in sharedpreference to track user is loggedin or not
public static final String LOGGEDIN_SHARED_PREF = "loggedin";
登录的PHP文件
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$email = $_POST['email'];
$password = $_POST['password'];
$id = $_POST['id_utilizador'];
//Creating sql query
$sql = "SELECT * FROM Utilizadores WHERE email='$email' AND password='$password'";
//importing dbConnect.php script
require_once('connection.php');
//executing query
$result = mysqli_query($con,$sql);
//fetching result
$check = mysqli_fetch_array($result);
//if we got some result
if(isset($check)){
//displaying success
echo "success";
}else{
//displaying failure
echo "failure";
}
mysqli_close($con);
}
包含我想要使用的查询的PHP文件(我希望为特定会话userId付出代价。
<?php
class Historico {
private $db;
private $connection;
function __construct() {
$this ->db = new DB_Connection();
$this ->connection = $this->db->getConnection();
}
public function guardar_historico($custos){
$query = "insert into Custoss (custos) values ( '$custos')";
$inserted = mysqli_query($this -> connection, $query);
if($inserted == 1 ){
$json['success'] = 'Custos guardado';
}else{
$json['error'] = 'Custos nao guardado';
}
echo json_encode($json);
mysqli_close($this->connection);
}
}
$historico = new Historico();
if(isset($_POST['custos'])) {
$custos = $_POST['custos'];
if(!empty($custos)){
$historico-> guardar_historico($custos);
}else{
echo json_encode("you must type both inputs");
}
}
?>