Login with verification of two tables

Asked

Viewed 325 times

5

Good afternoon, I’m developing a site, and I’m having a lot of doubts lately, it’s for my tcc and they help little.

I made a system to register the user being a student or teacher with Mysql, in different tables. Example: if the user is a student, he enters the student registration tab and this data is sent to the student table, as well as if the user is a teacher. So far so good.

But with the login system, I need him to check the email and password of the two tables (student and teacher); I’m having trouble with it, because the way I did it returns incorrect email or password.

Follow php code below user validation:

 <?php

 session_start();
 include("conexao.php");

 $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
 $senha = filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING);

 /* Verifica se existe usuario, o segredo ta aqui quando ele procupa uma
 linha q contenha o login e a senha digitada */
 $sql_logar = "SELECT aluno.email, aluno.senha, professor.email, professor.senha 
 FROM aluno, professor WHERE aluno.email AND professor.email = '$email' AND aluno.senha AND professor.senha = '$senha'";
 $exe_logar = mysqli_query($conection, $sql_logar) or die (mysqli_error($conection));
 $num_logar = mysqli_num_rows($exe_logar);

 //Verifica se n existe uma linha com o login e a senha digitado
 if ($num_logar == 0){

     $_SESSION['msg'] = "<div class='alert alert-danger'>Login ou senha incorreto!</div>";
     header("Location: index.php?login");
 }
 else{
     //Cria a sessão e verifica tipo de login
     $informacao = mysqli_fetch_assoc($exe_logar);

     $_SESSION['tipo'] = $informacao ['tipo'];
     $_SESSION['email'] = $informacao ['email'];
     $_SESSION['senha'] = $informacao ['senha']; 
     $_SESSION['nome'] = $informacao ['nome'];


     if($_SESSION['tipo'] != "2"){
         header("Location: aluno.php");
     }else{
     header("Location: professor.php");
     }
 }
?>

Note in the Mysql connection below that I tried to carry out with it to select the respective emails and passwords of the two tables, but it did not work, it returns that the login data entered by the user are incorrect.

$sql_log = "SELECT student.email, student.password, teacher.email, teacher.password FROM student, teacher WHERE student.email AND teacher.email = '$email' AND student.password AND teacher.password = '$password'";

If anyone knows any better way to do it.

  • 1

    From php I can not help but I would make a users table, and another user_profile and this second would control users, I think the modeling is better so

  • I thought the same thing @rnd_rss, but I wanted to organize more completely, so that when I want to see which teachers I don’t need to search in the user table.

  • 2

    I understand, it would be good to take a look at data normalization so you understand better why separate, and if it is to be "easier" puts everything in one table and creates a profile column

  • All right, if no one can help, I’ll do it this way!

  • Redo the modeling because even fixing the query the problem generated is much bigger: SELECT aluno.email, aluno.senha, professor.email, professor.senha &#xA; FROM aluno, professor WHERE (aluno.email = '$email' OR professor.email = '$email') AND (aluno.senha = '$senha' OR professor.senha = '$senha'); .I’m sorry to use the term, but this is known informally as the Gambi style of encoding(gambiarra) and it’s not a good thing. It’s like a snowball where every new feature you need to use an ever-increasing code to clean up the problem created at the root.

  • Until it worked @Augustovasques, but as you said, it entails several problems, I think I will solve all this with just one table. There’s not much to do.

Show 1 more comment

1 answer

2


Problem solved as follows. I created a users table to generalize all registered in the system, so. I was assigned a new field with the name "type" to determine what would be the function of the user on the site.

$sql_logar = "INSERT INTO usuarios (nome, senha, confsenha, email, nomecomp, matricula, tipo)

Along with that, I also modified that, in the end, add a number to determine the function, being possible the change in the future.

VALUES ('$usuario_aluno', '$senha_aluno', '$confsenha_aluno', '$email_aluno', '$nomecomp_aluno', '$matricula_aluno', '1')";

Browser other questions tagged

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