Jump to content


Photo

Searching 2 tables in one query


  • Please log in to reply
2 replies to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 16 April 2006 - 10:56 AM

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:

$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
(
.........................

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??

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

The bit in bold is the bit i am supposed to be changing.
Thanks.


#2 arifsor

arifsor
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationPakistan

Posted 16 April 2006 - 07:06 PM

[!--quoteo(post=365271:date=Apr 16 2006, 05:56 AM:name=OriginalSunny)--][div class=\'quotetop\']QUOTE(OriginalSunny @ Apr 16 2006, 05:56 AM) View Post[/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:

$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
(
.........................

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??

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

The bit in bold is the bit i am supposed to be changing.
Thanks.
[/quote]

Check this

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

Arif Sorathia

---------------------------
Power Demand Respect
Arif Sorathia

#3 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 17 April 2006 - 01:17 AM

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??




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users