我正在寻找一种通过使用两个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>";
}
}
我将跳过以下事实:
- 不应该以纯文本形式存储密码;请改用bcrypt或
password_hash()
- 应该使用准备好的语句(PDO/mysqli),并放弃使用不推荐使用的
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注入安全的,请使用mysql_real_escape_string()
!