Jump to content

How to check two tables


Cheese

Recommended Posts

I have two tables one called students and another called staff and when you login I need a query to be run to check both tables for a valid login and display a message to welcome the user hence why I also need first name.

 

I have tried doing the bellow sql query but I think all this does is check both tables for the same details and only lets you login if a match is found in both and not one or the another.

 

 
$q = "SELECT students.student_id, staff.staff_id, students.first_name, staff.first_name, students.user_level, staff.user_level 
FROM students, staff 
WHERE (students.email='$e' AND students.pass=SHA1('$p')  OR staff.email='$e' AND staff.password=SHA1('$p'))";

 

Any help would be appreciated.

Link to comment
Share on other sites

$q = "SELECT students.student_id, staff.staff_id, students.first_name, staff.first_name, students.user_level, staff.user_level 
FROM students, staff 
WHERE (students.email='$e' AND students.pass=SHA1('$p'))  OR (staff.email='$e' AND staff.password=SHA1('$p'))";

Link to comment
Share on other sites

Hi

 

While in every way agreeing with PFMaBiSmAd, you could do it with:-

 

SELECT 'Student' AS LoginType, students.student_id, students.first_name, students.user_level
FROM students
WHERE students.email='$e' AND students.pass=SHA1('$p'
UNION
SELECT 'Staff' AS LoginType, staff.staff_id, staff.first_name, staff.user_level 
FROM staff 
WHERE staff.email='$e' AND staff.password=SHA1('$p'))

 

That should give you the row along with whether it is a student or a staff table row.

 

What your current code appears to be doing is a cross join, giving you every combination of rows from the 2 tables (ie, 100 students and 100 staff would generate 10000 rows), and then finding those where the email and password match (so if one student matched you would get 100 rows for that student, one for each member of staff).

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.