Php PDO与数据库的连接

I have database with 2 table. Students and Profesors. I create one more login table and there are all email and passwords from students and profesors. I want to create that Student try to login that will send him on some student.php and when profesor try to login it will send him on profesor.php

I try with this code, but it always return me 1. So this is not good options..

if(isset($_POST['Submit'])){

$sql= "SELECT COUNT (*) FROM `students` AND 'Profesors' WHERE `username` = :username and `password` = :password ";
$result = $connection->prepare($sql);
$result->bindParam(":username" ,$_POST['username']);
$result->bindParam(":password" ,$_POST['password']);
$result->execute();

$num=$result->rowCount();
if($num > 0){
    header("location:index.php");
}else{
    header("location:login.php");
}

I need some idea, how to change my datebase or this login code.

I would personally not let professors and student use the same login. Now to answer the question I would change my query to the following:

SELECT user_type FROM `login_table` 
WHERE `username` = :username AND `password` = :password 

once query return result I would check the user_type field and redirect accordingly.

$num=$result->rowCount();
if($num > 0){
   $data=$result->fetch();
   if($data['professor']){
      header("location: professor.php");
   }else{
      header("location: student.php");
   }
}else{
    header("location:login.php");
}

Here's an idea of how you might want to structure this Have a table defining types of accounts, maybe

account_type_id | account_type_desc | account_type_dest

Where the first is unique key, the second is "Professor" "Student" or anything else you add, and the third is where you want to send that type of user. "student.php", "professor.php", etc

Then your user table would be something like

account_id | account_type_id | email | password

Then you can query the user table for a user matching the person trying to login in, authenticate their password, grab the account type id and turn it against the table of account types and then you can act based on that information

You can do this using a subquery and union statements. You can create a single table out of the two using a union statement

$subquery = "SELECT username,password FROM `students` UNION SELECT username,password FROM `Profesors`";

Then run a query using the subquery as your source table

$sql= "SELECT * FROM (" . $subquery . ") WHERE `username` = :username and `password` = :password ";

Then use rowCount to determine how many rows were returned.

You want to use php session or issue a cookie to the user for a proper and secure user login.

In your query I think you wanted:

$sql= "SELECT COUNT (*) FROM `students` AND 'Profesors' WHERE `username` = ".$_POST['username']." and `password` = ".$_POST['password']." ";