How to show certain data depending on who logged in

Asked

Viewed 190 times

1

I have a question about login for several people. I have this code:

<?php
    include("conectar.php");
    $usuario = $_POST['usuario'];
    $senha = $_POST['senha'];

    $query = "
        SELECT count(*)
        FROM usuarios
        WHERE usuario = '" . $usuario . "' AND senha = '" . $senha . "'";
    $consulta = mysql_query($query);
    $resultado = mysql_fetch_array($consulta);

    if ($resultado[0]) {
        echo "Login realizado com sucesso!";
    }
    else {
        echo "Dados incorretos.";
    }

?>

Each Login will have a specific data to display from a table tb_company. When logging in, I need to show this worker’s data. I know that it is necessary to do a query for each user. If login is done by Y, do query Y and display data Y. If login is done by Z, do query Z and display data Z. But I don’t know how to do this.

This same data is found in a Mysql database. The only data that can be related is the ID_empresa and ID_usuario.

  • Does the answer solve your problem? If yes, mark it as right.

  • Calm down, Jorge :)

1 answer

5

I edited your SQL a little according to the information passed by the comment and was like this:

(Don’t forget to change the SQL to your case, the fields I placed fictitiously wondering how would be in your database).

Remember, when you use quotes ("test $data test"), you don’t need to break it to add a variable ("test". $data . "test"), in this case PHP will understand that there is a variable somewhere and will replace it.

$usuario = 'teste';
$senha = 'teste2123';
$querySQL = "SELECT * FROM usuarios AS usu LEFT JOIN tb_empresa AS emp ON emp\.id_usuario = usu\.id WHERE usuario = '$usuario' AND senha = '$senha'";

var_dump($querySQL);
$query = mysql_query($querySQL);

if (!($row = mysql_fetch_array($query)))
{
    echo 'Usuário não encontrado!';
} else {
    echo 'Usuário encontrado!';
    var_dump($row); // Os dados estarão todos aqui, só utilizar como deseja!.
}

Test and inform us of the result;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.