[英]PHP login using details from two mysql tables
我通过使用两个mysql表中的详细信息寻找登录方式。 我做了一个代码,一切正常,但是当我登录时显示电子邮件或密码不正确的消息,然后成功登录。
这是我的代码:
if(isset($_POST['submit']))
{
$email = $_POST['Email'];
$password = $_POST['password'];
$checkUser1 = "SELECT * FROM table1 where theEmail = '$email' AND thePass = '$password' ";
$checkUser2 = "SELECT * FROM table2 where theEmail = '$email' AND thePass = '$password' ";
$run = mysql_query($checkUser1,$checkUser2);
if (mysql_num_rows($run) > 0){
$_SESSION['Email']= $email;
echo "<script>window.open('HomePage.php','_self')</script>";
} else {
echo "<script>alert('incorrect email or password !')</script>";
}
}
我要跳过你的事实:
password_hash()
。 mysql_
函数; 或者确保逃避所有用户输入。 你可以简单地使用JOIN:
$checkUser = sprintf("SELECT *
FROM table1
INNER JOIN table2 USING (theEmail)
WHERE table1.theEmail = '%s' AND table1.thePass = '%s'",
mysql_real_escape_string($email),
mysql_real_escape_string($password)
);
mysql_query($checkUser) or die("Oops!");
您不能通过一个mysql_query运行两个查询。 你需要执行两次mysql_query
:
$run = mysql_query($checkUser1);
$run2 = mysql_query($checkUser2);
if (mysql_num_rows($run) > 0) {
$_SESSION['Email']= $email;
echo "<script>window.open('HomePage.php','_self')</script>";
} else {
echo "<script>alert('incorrect email or password !')</script>";
}
但更好的是,只执行一个查询(例如,您需要将其调整到您的数据库):
$run = mysql_query("SELECT * FROM table1, table2 WHERE table1.theEmail = '".mysql_real_escape_string($email)."' AND table1.thePass = '".mysql_real_escape_string($password)."' AND table1.id = table2.id;");
你的代码也不是sql-injection-safe,请使用mysql_real_escape_string()
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.