1

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

4
  • is your password encrypted ?? Commented May 31, 2017 at 4:45
  • No no, I have it simple with POST and with type "password" in the HTML Commented May 31, 2017 at 4:55
  • then just put and AND instead of comma after email = $email and password=$password like this in the 2nd query Commented May 31, 2017 at 4:57
  • you are welcome, and dont verify an answer and then unverify it, it feels bad. both the answers given are correct. so you should judge the verified ans according to the time bro. anyways cheers Commented May 31, 2017 at 5:16

3 Answers 3

1

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;

 }
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thanks, also needed to use mysql_num_rows($query2) for the verification in the if
1
  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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.