Jump to content

Undefined Variable


Nikk

Recommended Posts

Hey guys, I've been coding PHP for about two weeks now, so excuse the appalling code. Here is the error message

Notice: Undefined variable: dbusername in C:\wamp\www\twitbiscuit\process.php on line 25

 

Here is a pastie link to my code, thanks in advance.

http://pastie.org/1076001

 

( The fact my first post is asking for help, fail. )

Link to comment
https://forums.phpfreaks.com/topic/209832-undefined-variable/
Share on other sites

 $row = mysql_fetch_assoc($query);
         $dbusername = $row['username'];
         $dbpassword = $row['password'];

 

should be:

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

Link to comment
https://forums.phpfreaks.com/topic/209832-undefined-variable/#findComment-1095298
Share on other sites

A notice is nothing realy to worry about but ideally you'd want to strive to eliminate them.

 

All it means is that you're accessing something [variable] that hasn't yet been set.

 

In your case one or more of those variables are being referenced without being defined.

Link to comment
https://forums.phpfreaks.com/topic/209832-undefined-variable/#findComment-1095303
Share on other sites

Sorry, mgallforever, still half dead. Long day at work and I wasn't paying attention very well.

 

As for the problem at hand you may want to put your if ($username==$dbusername&&$password==$dbpassword) statement into the if ($username&&$password) statement:

 

//setting the first variables
$username = $_POST['username'];
$password = $_POST['password'];


if ($username&&$password)
{
    // connecting
    $connect = mysql_connect("localhost","root","") or die ("couldn't connect");
    mysql_select_db("phplogin") or die ("no sign of db");

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

    $numrows = mysql_num_rows($query);

        $row = mysql_fetch_assoc($query);
         $dbusername = $row['username'];
         $dbpassword = $row['password'];
     
     // check to see if they match!
     if ($username==$dbusername&&$password==$dbpassword)
        echo "logged in! <a href='member.php'>Click</a> view your live feed";
     else
         echo "Login failed, check username and password";

}

Link to comment
https://forums.phpfreaks.com/topic/209832-undefined-variable/#findComment-1095305
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.