Cheygipe
Cheygipe

Reputation: 13

MySQL Retrieve Password field from database using email field

I'm new using php and I'm making a login window for my page where I want to compare if the email and password from the inputs are the same to those in the database; I already have the comparison for the email but I don't know how to retrieve the exact password for that email and compare it with the password input to know if it's the same. This is what I have for my 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;

        }




    }

By the way, thank you for your help

Upvotes: 1

Views: 1909

Answers (3)

Rajesh Pradhan
Rajesh Pradhan

Reputation: 210

  1. you are using "," instead of "AND". use and it shall solve
  2. after executing 2nd query your data will be in $query. you have use

    $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;
            }
    

hope you get answer.

Upvotes: 1

Arshad Shaikh
Arshad Shaikh

Reputation: 564

Pleas try this

 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;

 }

Upvotes: 1

B. Desai
B. Desai

Reputation: 16436

Change query like below

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

Also use mysqli* because mysql* is deprecated and completly removed in Php 7

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

Upvotes: 1

Related Questions