954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Login Code Problem

I'm having problem with my login php files.

I don't know if it is in the passing of values from inputboxes or connection with my database records.

log-in code:

<td align="right" width="130px">
    Username:
</td>
<td><input type="text" size="40" id="username" class="inputbox" /></td>
 </tr>
<tr>
<td align="right">
 Password:
</td>
<td><input type="password" size="40" id="password" class="inputbox" /></td>


Login checking code:

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";


And the line producing the error is this:

$myusername=isset($_POST['myusername']) ? $_POST["myusername"] : ""; 
$mypassword=isset($_POST['mypassword']) ? $_POST["mypassword"] : "";


Error:

Fatal Error:Undefined index: myusername
Undefined index: mypassword


The myusername in POST method is same name of my inputboxes on the login page same as mypassword.

Tried isset but still won't get the right output.
And i'm entering the correct records from my DB.

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

I have noticed something wrong with your form.

<td align="right" width="130px">
    Username:
    </td>
    <td><input type="text" size="40" id="myusername" class="inputbox" /></td>
    </tr>
    <tr>
    <td align="right">
    Password:
    </td>
    <td><input type="password" size="40" id="mypassword" class="inputbox" /></td>

OK. Change the IDs for password to mypassword and for username to myusername. Try this and I'm sure it will work. The reason I'm telling you to do so Because you have got different names that don't match the names in your query.

rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

Done but still..

Getting the message in the echo part.

echo "Wrong Username or Password";


I'm using correct username and password from DB and DB name and tables are declared correctly

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

Change the attribute ID to name in your form. This will fix the problem. If it doesn't, then try the steps below:

Delete the $ sign before table name in your query

$sql="SELECT * FROM tbl_name WHERE username='$myusername' and password='$mypassword'";
if $count => 1


then Try checking if the username and password are typed.

$mypassword = $_POST['mypassword'];
$myusername = $_POST['myusername'];

if ( isset('$mypassword') && isset('$myusername') ){

DO SOMETHING 

}else{

echo "username: $myusername and password: $mypassword  are not correct";

}
rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

if this solves your problem, please kindly mark it as a solved thread.

rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

Try to use instead of id="myusername" use name="myusername", depending how you processing the form and the server you are using, sometimes cause problems. $_POST is normally set by the name of the input not the id. In some systems a nameless input is considered disabled an pass no values.

You can try seeing what's the post with:

print_r($_POST)
raphie
Light Poster
44 posts since Jun 2009
Reputation Points: 13
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You