Jump to content

Searching 2 tables in one query


OriginalSunny

Recommended Posts

Hi,
I am creating a login page for an employee however the administrator should also be able to access the page. I have got it working for the employee to be able to access the page with a username and password using:

[i] $sql = "SELECT Username FROM Employee
WHERE Username='$_POST[username]'";
$result = mysql_query($sql)
or die("Couldn't execute query.");
$num = mysql_num_rows($result);
if ($num == 1) // login name was found
{
$sql = "SELECT password FROM Employee
WHERE Username='$_POST[username]'
AND password='$_POST[password]'";
$result2 = mysql_query($sql)
or die("Couldn't execute query 2.");
$num2 = mysql_num_rows($result2);
if ($num2 > 0) // password is correct
(
.........................[/i]
Now how do i alter it so that an administrator from the admin table can also access the page??
I am thinking of using this but i know it wont work so there must be another way??
[i]
$sql = "SELECT Username FROM Employee [b]OR Admin[/b]
WHERE Username='$_POST[username]'";
$result = mysql_query($sql)
or die("Couldn't execute query.");
..............................[/i]
The bit in bold is the bit i am supposed to be changing.
Thanks.
Link to comment
Share on other sites

[!--quoteo(post=365271:date=Apr 16 2006, 05:56 AM:name=OriginalSunny)--][div class=\'quotetop\']QUOTE(OriginalSunny @ Apr 16 2006, 05:56 AM) [snapback]365271[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi,
I am creating a login page for an employee however the administrator should also be able to access the page. I have got it working for the employee to be able to access the page with a username and password using:

[i] $sql = "SELECT Username FROM Employee
WHERE Username='$_POST[username]'";
$result = mysql_query($sql)
or die("Couldn't execute query.");
$num = mysql_num_rows($result);
if ($num == 1) // login name was found
{
$sql = "SELECT password FROM Employee
WHERE Username='$_POST[username]'
AND password='$_POST[password]'";
$result2 = mysql_query($sql)
or die("Couldn't execute query 2.");
$num2 = mysql_num_rows($result2);
if ($num2 > 0) // password is correct
(
.........................[/i]
Now how do i alter it so that an administrator from the admin table can also access the page??
I am thinking of using this but i know it wont work so there must be another way??
[i]
$sql = "SELECT Username FROM Employee [b]OR Admin[/b]
WHERE Username='$_POST[username]'";
$result = mysql_query($sql)
or die("Couldn't execute query.");
..............................[/i]
The bit in bold is the bit i am supposed to be changing.
Thanks.
[/quote]

Check this

[code]
SELECT USERNAME FROM EMPLOYEE E, ADMIN A WHERE E.USERNAME = $_POST['username'] OR A.USERNAME = $_POST['username']
[/code]

Arif Sorathia
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.