bring information according to the user logged in mysql

1

I have a database with two tables, related to each other by ID. The T1 contains the entire record of a primary user, and T2 contains alternative users linked to the main user ID.

What I need to do is that when an alternate user logs in, the TOKEN of the alternate user is loaded instead of the TOKEN of the main user, in order to automatically register the User code that started session.

Table 1 main user

id |  usuario  | password | token |
-----------------------------------
1  | Andres    | *****    | e12A1 |
-----------------------------------
2  | Maria     | *****    | HJb71 |
-----------------------------------

Table 2 alternative users

id |  usuario  | password | token | id_ppl |
--------------------------------------------
1  | Juana     | *****    |       | 1      |
--------------------------------------------
2  | Martin    | *****    |       | 1      |
--------------------------------------------

PHP = > Here I need to replace the token of the user ppl with the token of the alternative user.

$stmt = $conn->prepare("SELECT T1.* FROM escolar AS T1 LEFT OUTER JOIN users_extra AS T2 ON T1.id = T2.id_ppl");

    $stmt->bind_param("ss",$usuario, $password); 
     $stmt->execute(); 
     $stmt->store_result(); 
     if($stmt->num_rows > 0){ 
     $stmt->bind_result($id, $usuario, $token);
     $stmt->fetch();

    $user = array(
     'id' => $id,
     'usuario' => $usuario,
     'token' => $token
     );

My attempt was to do something like this:

$stmt = $conn->prepare("SELECT T1.* 
(SELECT T1.token FROM escolar AS T1 LEFT OUTER JOIN users_extra AS T2 ON T1.token = T2.token) AS token_alt
FROM escolar AS T1 LEFT OUTER JOIN users_extra AS T2 ON T1.id = T2.id_ppl");

But I did not know how to continue, I'm learning PHP I'm a novice , and I thought that maybe when I get the alternative token, I could replace it with the main token.

Thanks for the help

    
asked by Andres Arango 30.07.2018 в 03:56
source

1 answer

0

Let's clarify a couple of things first;

  • Do you need an alternate user to log in to make a request to the database and return the toke of this one? The token of this alternative user?
  • Taking into account the above, do you need any data from the main user? Or nothing?
  • It is possible that you were wrong and what you need is that when the alternative user logs in, return the token of the main user it belongs to?

As I understood this is my solution ( I will change the answer if it turns out not to be ):
Make a simple query to which you pass the id of the user that you need the token, in php it would be such that:

$idUser; // Variable que contiene el id del usuario del cual necesitas le token
$sql = "SELECT token FROM users_extra WHERE id=$idUser";
$stmt = $conn->prepare($sql);
    
answered by 02.08.2018 в 11:54