0

I have my data in MySQL database is

'1', 'Demo', 'Demo1', 'Test'

On line 'echo $row[2]' it gives error "Notice: Undefined offset: 2"

Any solutions..?? PHP Code is as below...

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
    <?php

    $uname=$_POST['txtUserName'];
    $passwd=$_POST['txtPassword']; 

    $query="SELECT usernm,passwd FROM tbuserdemo WHERE usernm='$uname'";
    $result=  mysql_query($query);
    $rows=  mysql_num_rows($result); 

    if($rows==0)
    {
        echo "User Name is Wrong";
    }
    else
    {
        $row=mysql_fetch_row($result);
        $encpasswd=  encrypt(1, $passwd);        


        if ($row[1]==$encpasswd)
        {            
            echo "Welcome $uname";
            echo $row[2];
        }
        else
        {
            echo "Password is Wrong";
        }
    }


    ?>
</body>
</html>
2

6 Answers 6

3

You're only fetching 2 fields from the database, SELECT usernm,passwd so you should only have $row[0] and $row[1] with values. Array indexes are zero-based.

Sign up to request clarification or add additional context in comments.

Comments

0

Welcome to programming. Many languages do this, Array indexes start at 0 NOT 1. So you need to do

if ($row[0]==$encpasswd)
        {            
            echo "Welcome $uname";
            echo $row[1];
        }

Comments

0

It'd be $row[0] and $row[1]. Remember, PHP's arrays are zero-based. To get a $row[2] you'd have to be selecting THREE fields in your query.

Comments

0

change to this :

   if ($row[0]==$encpasswd)
    {            
        echo "Welcome $uname";
        echo $row[1];
    }
    else
    {
        echo "Password is Wrong";
    }
}


?>

first index in the array is in location 0 (not 1)

Comments

0

The arrays begin with 0.

Try this:

    if ($row[0]==$encpasswd)
    {            
        echo "Welcome $uname";
        echo $row[1];
    }
    else
    {
        echo "Password is Wrong";
    }

Comments

0

You might want to read up a bit on SQL Injection attacks. At least if you intend to publish that code outside a development/testing environment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.