undifined indx

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

undifined indx

Post by shehan31 »

<?php Session_start(); ?>
<html>
</head>

<body>
<form action='auth.php?login=yes'method=POST>
Username: <input type=text, name='user'><br/>
Password: <input type=password name='pass'><br/>
<input type=submit name='login'value='go!'>
</form>
<?php
$user=$_POST['user'];
$pass=$_POST['pass'];
$login=$_GET['login'];


if ($login=='yes'){


$con = mysql_connect ("localhost","root","") or die ("error");
mysql_select_db ("login") or die ("eroor connecting Database");
$get=mysql_query("SELECT count(id)FROM login WHERE user= '$user' AND pass='$pass'");
$result=mysql_result($get,0);
mysql_close($con);
if($result!=1)echo "Login failiure";
else{
echo "Login Sucessfull";
$_SESSION['user']=$user;

}
}


$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
?>
</body>
</html>

hi all:
this is a script for web page login. I have taken this for a you tube viedo. I have created three parameters in my database table.
Database name: login
tablename:login
filename : auth.php
three fields are exactley (id,user,pass).
when i compile it, the webpage will be dispalyed with three notices and they are:
Notice: Undefined index: user in C:\wamp\www\guestbook\auth.php on line 12

Notice: Undefined index: pass in C:\wamp\www\guestbook\auth.php on line 13

Notice: Undefined index: login in C:\wamp\www\guestbook\auth.php on line 14

but when i enter a user name and a password, it will work as expected with dissapered notices.
i want to stop these notices appearing when i compile it.
can any one help?
Thank you
Shehan
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: undifined indx

Post by oscardog »

Ask your host to turn off error_reporting on your server (or more accurately only display parse/fatal errors).
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: undifined indx

Post by shawngoldw »

oscardog wrote:Ask your host to turn off error_reporting on your server (or more accurately only display parse/fatal errors).
I absolutely disagree with this statement. If you are getting warnings that means you are doing something wrong, you should not turn off error reporting to make them go away; you should fix it. After the site is completely built and ready to go live then ALL error reporting should be turned off.

Here is you're problem,

Code: Select all

<form action='auth.php?login=yes'method=POST>
Username: <input type=text, name='user'><br/>
Password: <input type=password name='pass'><br/>
<input type=submit name='login'value='go!'>
</form>
<?php
$user=$_POST['user'];
$pass=$_POST['pass'];
$login=$_GET['login'];


if ($login=='yes'){
The problem is that you are trying to access $_POST['user'], $_POST['pass'], and $_GET['login'] when those indexes of the arrays have never been set.

Change that code to this:

Code: Select all

<form action='auth.php' method=POST>
Username: <input type=text, name='user'><br/>
Password: <input type=password name='pass'><br/>
<input type=submit name='login' value='go!'>
</form>
<?php
if (isset($_POST['login']){
$user=$_POST['user'];
$pass=$_POST['pass'];
First, I removed "?login=yes from the action, it is just unnecessary.
Since we are not using $_GET['login'] the condition of the if has to change. So I changed it so that it just checks if $_POST['login'] is set because that is the name of the submit button and it will confirm that the form has been submitted.
Then to fix the problem that you came here for, I moved the $user and $pass declarations inside the if statement since they will only exist if the form has been submitted.

Shawn
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: undifined indx

Post by shehan31 »

shawngoldw wrote:
oscardog wrote:Ask your host to turn off error_reporting on your server (or more accurately only display parse/fatal errors).
I absolutely disagree with this statement. If you are getting warnings that means you are doing something wrong, you should not turn off error reporting to make them go away; you should fix it. After the site is completely built and ready to go live then ALL error reporting should be turned off.

Here is you're problem,

Code: Select all

<form action='auth.php?login=yes'method=POST>
Username: <input type=text, name='user'><br/>
Password: <input type=password name='pass'><br/>
<input type=submit name='login'value='go!'>
</form>
<?php
$user=$_POST['user'];
$pass=$_POST['pass'];
$login=$_GET['login'];


if ($login=='yes'){
The problem is that you are trying to access $_POST['user'], $_POST['pass'], and $_GET['login'] when those indexes of the arrays have never been set.

Change that code to this:

Code: Select all

<form action='auth.php' method=POST>
Username: <input type=text, name='user'><br/>
Password: <input type=password name='pass'><br/>
<input type=submit name='login' value='go!'>
</form>
<?php
if (isset($_POST['login']){
$user=$_POST['user'];
$pass=$_POST['pass'];
First, I removed "?login=yes from the action, it is just unnecessary.
Since we are not using $_GET['login'] the condition of the if has to change. So I changed it so that it just checks if $_POST['login'] is set because that is the name of the submit button and it will confirm that the form has been submitted.
Then to fix the problem that you came here for, I moved the $user and $pass declarations inside the if statement since they will only exist if the form has been submitted.

Shawn
the problem remains same.
Post Reply