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.

Recommended Answers

All 5 Replies

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.

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

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";

}

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

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)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.