-2

Only the last line of if/else statement is working. How do i solve this? the result is Please enter a user name and password

the following is the php code :

<?php

session_start();

$username =@$_POST['username'];
$password =@$_POST['password'];

if ($username&&$password) 
{
    $connect = mysql_connect("localhost","root","")or die("Couldn't connect to the database!");
    mysql_select_db("login") or die("Couldn't find database!");

    $query = mysql_query("SELECT * FROM users WHERE username='$username'");

    $numrows = mysql_num_rows($query); 

    if($numrows!==0)
    {
        while($row = mysql_fetch_assoc($query))
        {
            $dbusername = $row['username'];
            $dbpassword = $row['password'];        
        }

        if($username==$dbusername&&$password==$dbpassword)
        {
            echo "Your are logged in!";
            $_SESSION['username']=$username;
        }
        else 
            echo "Your password is incorrect!";

    }
    else
        die("That user doesn't exists!");

}
else
    die("Please enter a user name and password");
?>

This is what I've got as my HTML form:

<html> 
    <form action="loginpage.php" method="post">
        Username: <input type="text" name"username">
        <p> 
            Password: <input type="password" name"password">
                <p> 
                    <input type="submit"> 
                </form> 
            </html>
Bono
  • 4,757
  • 6
  • 48
  • 77
Big Alex
  • 23
  • 4
  • Where is your code??? – Saty Aug 17 '15 at 07:27
  • Anybody have their crystal ball on them? I left mine at home. – Andrei Aug 17 '15 at 07:27
  • i had put in the code, but is not showing up here? okay thanks hanky for the edit :) – Big Alex Aug 17 '15 at 07:30
  • @BigAlex I've added your HTML code, but it seems like you forget to close quite a lot of tags there. You're not closing any of your `

    ` tags, and you should also close your input tags (can be done like so: ``

    – Bono Aug 17 '15 at 07:58

1 Answers1

-1
            <?php
            session_start();

            $username = @$_POST['username'];
            $password = @$_POST['password'];

            if (!empty($_POST['submit'])) {
                $connect = mysql_connect("localhost", "root", "")or die("Couldn't connect to the database!");
                mysql_select_db("login") or die("Couldn't find database!");

                $query = mysql_query("SELECT * FROM users WHERE username='$username'");

                $numrows = mysql_num_rows($query);

                if ($numrows > 0) {
                    while ($row = mysql_fetch_assoc($query)) {
                        $dbusername = $row['username'];
                        $dbpassword = $row['password'];
                    }

                    if ($username == $dbusername && $password == $dbpassword) {
                        echo "Your are logged in!";
                        $_SESSION['username'] = $username;
                    } else
                        echo "Your password is incorrect!";
                } else
                    die("That user doesn't exists!");
            }
            else {
                ECHO "Please enter a username and Password";
            }
            ?>
            <form method="post">
                <input type="text" name="username">
                <input type="text" name="password">
                <input type="submit" name="submit" value="submit">
            </form>

Try this But my suggestion would also be that you take if(!empty($_POST['submit'])) because when the document loads input fields will definitely be empty so it will go to the else part. where you have added the die code...avoid using die code in else use echo prompt there.

  • OMG, thanks, it works!. However would u tell me what is wrong with the previous script? Thanks! – Big Alex Aug 17 '15 at 08:08
  • as i told you when the document loads input fields will definitely be empty so it will go to the else part...It is always prefferred to use button into the if(!empty($_POST['submit button name'])) to avoid these things. Thanks If you like do mark as my answer as ticked. – Manjot Singh Aug 17 '15 at 08:18
  • i cant upvote. btw i still cant seems to edit with my code to work. – Big Alex Aug 17 '15 at 08:35