Page 1 of 2

Undefined Index error in $_REQUEST

Posted: Thu Aug 11, 2005 3:16 am
by waqas_punjabian
Hi gals n guys,,,

i m facing two strange errors in all variables and all pages , but i 'll ask about one page and if this problem will be solved here than i 'll be able to solve it in all pages .

the"findbooks.php" there i 've used

$var_ac = $_request[action];

and i m getting two errors while executing this file on local server i.e) IIS ...

First error is :

Notice: Use of undefined constant action - assumed 'action' in D:\projects\Got_Med_Books\theSite\www\findbooks.php on line 6
and Secound error is :

Notice: Undefined index: action in D:\projects\Got_Med_Books\theSite\www\findbooks.php on line 6

and the strange thing is when i uploaded this file to a live server, it worked perfiectly. there were not both of the errors ...

Wht the reason could be ???

Further more, i ve tried following things to make this error to be solved, but it did'nt work :


First : i 've tried to give it qouts like $var_ac = $_request['action']
then first error 've been solved ...

( but it will be too much time consuming to change in all vars and files)

Second : i 've tried to make some change in "php.ini" i.e)

register_globals = on

but it also did'nt work ...

plzz ,,, help me to work with these files at local server otherwise it would be terible to upload each file and test it on internet .

Thanks in ADVANCE

regards

Waqas

Posted: Thu Aug 11, 2005 3:22 am
by feyd
register_globals should be off.

the first "error" is from using action versus 'action' when you didn't have a constant defined as action. That's pretty straight forward to fix.

Your second "error" is because the element doesn't exist in the array you specified. Using isset() to check if the element exists before using it is always a good idea.

Do not take the band-aid approach and just turn off notices. ;)

y same code is working at internet ???

Posted: Thu Aug 11, 2005 5:00 am
by waqas_punjabian
But sir , the thing is, y the same code is working perfectly at online server. (at internet) ? if it is working there then may be i 'll be able to make it workable here ....

maybe i 'll have to make any change .... kindly tell me if u 've some idea ?

Take care

Bye

Re: y same code is working at internet ???

Posted: Thu Aug 11, 2005 6:04 am
by Roja
waqas@balianti.com wrote:But sir , the thing is, y the same code is working perfectly at online server. (at internet) ?
Its not working perfectly. The online server just turned off notices, so you dont SEE the notices that something is wrong.
waqas@balianti.com wrote:if it is working there then may be i 'll be able to make it workable here ....
He explained how to fix it correctly.

How can i turn off the notices ???

Posted: Thu Aug 11, 2005 10:29 am
by waqas_punjabian
Hi,

How can i turn off the notices ??? like at internet most of servers do it..

Bye

Re: How can i turn off the notices ???

Posted: Thu Aug 11, 2005 10:48 am
by Roja
waqas@balianti.com wrote:How can i turn off the notices ??? like at internet most of servers do it..
The notices are saying "Something is wrong". You don't want to turn them off, because something is wrong.

Fix the something that is wrong. We've already explained how to.

Posted: Thu Aug 11, 2005 10:48 am
by feyd
do not turn off notices. They still happen, it still slows php down. It's best to fix the problems, especially ones this simple.

hi

Posted: Fri Aug 12, 2005 1:58 am
by waqas_punjabian
one thing more i want to ask ,

U said that i should check my variable array in isset() ,..., ok that's fine but i m confuse about a couple of things . at start of the page the code is written as follows :

$action = $_REQUEST[action];

But by default, for the first time, when this page will be opened, i don't want this variable to be functional , but at the second time when user will visit this page this variable should be active.and then on the base of this action variable things are going to be extracted from database, and obviously all other functionality is being perform.

Wht should i do in default case ,..., that i don't want to use this variable and it should not give error ?

and Sir, i found $_GET or $_POST is perfect at the same place at default while i did'nt set any default value for that, but $_REQUEST is a very bad approach .

(And i m amazed it's going on perfectly on internet)

Anyway, Thanks a lot for ur Replies

Bye

isset()

Posted: Fri Aug 12, 2005 2:29 am
by AnarKy
Hi,
The isset(); function will help you decide which case it is:
1.Otherwise, the page has not been displayed and the variable has NOT been set.
2.If the variable has a value in it, then it is set (page has been displayed the second time).

Posted: Fri Aug 12, 2005 5:46 am
by bokehman
(isset($_REQUEST[action])) ? $action = $_REQUEST[action] : '' ;

Re: isset()

Posted: Fri Aug 12, 2005 5:48 am
by bokehman
AnarKy wrote:2.If the variable has a value in it, then it is set .
Not true! Both of the following return TRUE to isset() but have no value: $i = 0; $i = '';

Posted: Fri Aug 12, 2005 5:54 am
by feyd
those are valid values for isset to know the variable has been set. You may want to look at empty()

Posted: Fri Aug 12, 2005 6:04 am
by bokehman
feyd wrote:those are valid values for isset to know the variable has been set. You may want to look at empty()
To avoid the 'notice' isset() is needed not !empty()

Posted: Fri Aug 12, 2005 6:17 am
by feyd
bokehman wrote:
feyd wrote:those are valid values for isset to know the variable has been set. You may want to look at empty()
To avoid the 'notice' isset() is needed not !empty()
Oh, really? :twisted:

Code: Select all

<?php

if(empty($iDontExist))
{
	echo "I don't exist.";
}
else
{
	echo "I exist.";
}

?>
outputs

Code: Select all

I don't exist.
and guess what, the manual supports it:
empty() wrote:empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

Posted: Fri Aug 12, 2005 7:20 am
by bokehman
It looks like you are right. I though running if(empty($iDontExist)) would set the variable but on closer examination I have found it doesn't.