MySQL使用电子邮件字段从数据库中检索密码字段

时间:2017-05-31 04:42:23

标签: php mysql database

我是使用php的新手,我正在为我的网页创建一个登录窗口,我想要比较来自输入的电子邮件和密码是否与数据库中的相同;我已经对电子邮件进行了比较,但我不知道如何检索该电子邮件的确切密码,并将其与密码输入进行比较,以了解它是否相同。 这就是我对我的php:

$password=$_POST['password'];
$email=$_POST['email'];
$query = mysql_query( "SELECT email FROM Register WHERE email = '$email'");
$query2 = mysql_query( "SELECT password FROM Register WHERE email = '$email', password = '$password'");



    if(!$email)
    {
        if (!$password)
        {
            echo '
            <script>
            alert("Email and password are required.");
            window.history.go(-1);
            </script>';
            exit;
        }
        else
        {
            echo '
            <script>
            alert("Email Required.");
            window.history.go(-1);
            </script>';
            exit;
        }           
    }

    if (!$password)
    {
        echo '
        <script>
        alert("Password required.");
        window.history.go(-1);
        </script>';
        exit;
    }

    else
    {


        if(!mysql_num_rows($query))
        {
            echo '
            <script>
            alert("The email is not the same or does not exist");
            window.history.go(-1);
            </script>';
            exit;
        }

        if($query2==$password)
        {
            echo '
            <script>
            alert("Succesful Login");
            window.history.go(-1);
            </script>';
            exit;
        }

        else
        {
            echo '
            <script>
            alert("Password is not the same, please verify.");
            window.history.go(-1);
            </script>';
            exit;

        }




    }

顺便说一句,谢谢你的帮助

3 个答案:

答案 0 :(得分:1)

更改下面的查询

SELECT password FROM Register WHERE email = '$email' AND password = '$password'

也使用mysqli *,因为在Php 7中不推荐使用mysql *并完全删除

https://www.w3schools.com/php/php_ref_mysqli.asp

答案 1 :(得分:1)

请试试这个

 mysqli_query( "SELECT * FROM Register WHERE email = '$email' and password ='$password'");

if(mysql_num_rows($query))
{
            echo '
            <script>
            alert("Succesful Login");
            window.history.go(-1);
            </script>';
            exit;
 }
else
{
            echo '
            <script>
            alert("The email is not the same or does not exist");
            window.history.go(-1);
            </script>';
            exit;

 }

答案 2 :(得分:1)

  1. 你正在使用&#34;,&#34;而不是&#34; AND&#34;。使用,它将解决
  2. 执行第二次查询后,您的数据将在$ query中。你有使用

    $data=mysql_fetch_array($query);
    $data['password'];// there contains password
    so you code this
    if($query2==$password)
            {
                echo '
                <script>
                alert("Succesful Login");
                window.history.go(-1);
                </script>';
                exit;
            }
    will be changed to
    if($data['password']==$password)
            {
                echo '
                <script>
                alert("Succesful Login");
                window.history.go(-1);
                </script>';
                exit;
            }
    
  3. 希望你能得到答案。