Reputation: 35
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>
Upvotes: 0
Views: 89
Reputation: 349
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.
Upvotes: 0
Reputation: 5230
The arrays begin with 0.
Try this:
if ($row[0]==$encpasswd)
{
echo "Welcome $uname";
echo $row[1];
}
else
{
echo "Password is Wrong";
}
Upvotes: 0
Reputation: 9913
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)
Upvotes: 0
Reputation: 360872
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.
Upvotes: 0
Reputation: 1971
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];
}
Upvotes: 0
Reputation: 11840
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.
Upvotes: 3