简体   繁体   中英

How to select from multiple tables?

I'm making a website where and I need a login file to redirect administrators and clients to their respective account.

It is supposed to verify both tables and see if email corresponds to password.

Here's what I already have

<?php
include "data-base.php";
if($_SESSION){
header("location: index.php?site=perfil");
    echo "<script>window.location.href = \"index.php?site=login&erro=3\";</script>";
}
$result = mysql_query("select * from admin, clients where email='".$_POST['email']."' and password='".$_POST['password']."'");
if (mysql_num_rows($result)>0)
{
$linha = mysql_fetch_array($result);
$_SESSION['id']=$linha['id'];
if($linha['email'] = "admin") {
    header("location: admin.php?id=".$linha['id']."");
} else {
    header("location: client.php?id=".$linha['id']."");
}
} else {
echo "<script type=\"text/javascript\">alert(\"Incorrect email or password\"); window.location = 'index.php';</script>";
}
?>

I think it's better to slice one query on two parts. First, try to get rows from "admin"table. If mysql_now_rows == 0, then get rows from users. If empty, show the message about wrong login&password

$q = "SELECT * FROM admin WHERE email = '".$_POST["email"]."' AND password ='".$_POST["password"]."'";
$result = mysql_query($q);
if(sizeof($result) !== 0){
    header("put admin URL here");
} else {
    $q = "SELECT * FROM clients WHERE email = '".$_POST["email"]."' AND password ='".$_POST["password"]."'";
    $result = mysql_query($q);
    if(sizeof($result) !== 0){
        header("put clients URL here");
    } else {
        print_r("wrong email&pass");
    }
}

Sorry about formatting, I'm from phone

"select * from admin, clients where (admin.email='".$_POST['email']."' OR
clients.email='".$_POST['email']."') AND (admin.password='".$_POST['password']."' OR
clients.password='".$_POST['password']."')";

in this question, you also can use a column to enter user type rather than using two tables. if then can run below statement.

"select * from admin_clients_table where email='".$_POST['email']."' AND
password='".$_POST['password']."'";

Since question was how to select from multiple tables, you can do as follows:

"select table1.column1, table1.column2, table2.column1, table2.column3 from table1,
 table2 where (table1.column4 = table3.column1 and table2.column2 = 'value1') or
 table3.column1 = 'value2' order by table1.id desc limit 0,100";

There is a small error in your script.

if($linha["email"] = "admin")

should be

if($linha["email"] == "admin")

With 1 = all you do is saying $linha["email"] should become "admin" With 2 = you are comparing $linha["email"] and $linha["email"]

So in other words if $linha["email"] is the same as $linha["email"]

Hope this helps you on your way.

Also you need to change your query and join the to tables.

$result = mysql_query("SELECT * from admin, clients 
                       WHERE email='".$_POST['email']."' 
                       AND password='".$_POST['password']."'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM