[英]How to select from multiple tables?
我正在建立一个网站,我需要一个登录文件来将管理员和客户重定向到各自的帐户。
应该验证两个表,并查看电子邮件是否对应于密码。
这是我已经拥有的
<?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>";
}
?>
我认为最好将一个查询分为两个部分。 首先,尝试从“ admin”表中获取行。 如果mysql_now_rows == 0,则从用户那里获取行。 如果为空,则显示有关错误的登录名和密码的消息
$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");
}
}
抱歉格式化,我是电话人
"select * from admin, clients where (admin.email='".$_POST['email']."' OR
clients.email='".$_POST['email']."') AND (admin.password='".$_POST['password']."' OR
clients.password='".$_POST['password']."')";
在此问题中,您还可以使用一列来输入用户类型,而不是使用两个表。 如果可以,则可以在下面的语句中运行。
"select * from admin_clients_table where email='".$_POST['email']."' AND
password='".$_POST['password']."'";
由于问题是如何从多个表中进行选择,因此您可以执行以下操作:
"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";
您的脚本中有一个小错误。
if($linha["email"] = "admin")
应该
if($linha["email"] == "admin")
用1 =您所要做的就是说$ linha [“ email”]应该变成“ admin”用2 =您正在比较$ linha [“ email”]和$ linha [“ email”]
换句话说,如果$ linha [“ email”]与$ linha [“ email”]相同
希望这对您有所帮助。
另外,您需要更改查询并将表连接到表。
$result = mysql_query("SELECT * from admin, clients
WHERE email='".$_POST['email']."'
AND password='".$_POST['password']."'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.