Ask Experts Questions for FREE Help !
Ask
    mazunki's Avatar
    mazunki Posts: 1, Reputation: 1
    New Member
     
    #1

    Apr 4, 2012, 01:33 PM
    Php ask for help
    Hi. I have a problem here.
    When I write a username that doesn't exist I get no message up on the page.

    PHP Code:
    <?php

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

    if (
    $username&&$password
        {
            
    $connect mysql_connect("mysql.<MASKED>.com","<MASKED>","<MASKED>")or die(mysql_error());
            
    mysql_select_db("u875234194_login")or die(mysql_error());
            
    $query mysql_query("SELECT * FROM members WHERE username='$username'");
        
            
    $numrows mysql_num_rows($query)or die(mysql_error());
            
            if (
    $numrows!=0)
                {
                    while (
    $row mysql_fetch_assoc($query))
                        {
                            
    $dbusername $row['username'];
                            
    $dbpassword $row['password'];
                        }
                        
                        if (
    $username==$dbusername&&$password==$dbpassword)
                            {
                                echo 
    "You're logged in";
                            }
                        else
                            {
                                echo 
    "Wrong password";
                            }
                }
            else
                {
                    die(
    "That user doesnt exist");
                }
        }
    else
        {
                    
    ?>
                        <script type='text/javascript'>
                            alert("Please enter user and password");
                            history.go(-1);
                        </script>
                    <?php
        
    }
                    
    ?>
    If I change
    PHP Code:
    if (numrows!=0
    to
    PHP Code:
    if (numrows==0
    and write a username that does exist, I do get my That user doesn't exist message
    PHP Code:
    else { die("That user doesn't exist"
    rpray2007's Avatar
    rpray2007 Posts: 319, Reputation: 23
    Full Member
     
    #2

    Apr 4, 2012, 02:21 PM
    Change the while loop to this:

    Code:
     
    while ($row = mysql_fetch_assoc($query))
              {
                            $dbusername = $row['username'];
                            $dbpassword = $row['password'];
                  
                        
                        if ($username==$dbusername&&$password==$dbpassword)
                            {
                                echo "You're logged in";
                                break;
                            }
                        elseif ($username==$dbusername&&$password !=$dbpassword)
                            {
                                echo "Wrong password";
                                break;   
                            }
                     }
    The problem is that you are loading the dbusername/dbpassword with an empty row since you are checking after the while loop is over.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Update in PHP [ 2 Answers ]

This is my code.Please tell me how to update it.How can I get values individually so that I can update my database.please help me... <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("hai", $con);

Php graphics [ 1 Answers ]

Hi I want to ask whether it is possible to draw barcharts and piecharts by php.. How? :confused:

Authentication in PHP [ 3 Answers ]

Hai, I'am ne wto PHP and I am doing a project.I want a login,in such a way that each one entered should be identified (i.e,whether the entered person is a client or admin )and they should be directed to their respective pages.What is the code I have to use.anybody there to help me...


View more questions Search