Jump to content

Archived

This topic is now archived and is closed to further replies.

OriginalSunny

Searching 2 tables in one query

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Tried this but it comes up with the error "couldnt execute query".

$sql = "SELECT Username FROM Employee E, Admin A
WHERE E.Username='$_POST[username]' or A.Username='$_POST[username]'";
$result = mysql_query($sql)
or die("Couldn't execute query.");

Any idea why??

Share this post


Link to post
Share on other sites

×

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.