undefined index

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
jayymz
Forum Newbie
Posts: 4
Joined: Thu Jun 27, 2002 2:48 pm

undefined index

Post by jayymz »

why do I get an undefined index error when using this?

The idea is to use the variable $user in an insert query later on. The variable gets its info from a form post.

Code: Select all

$user = $_POSTї'user'];
anyone know?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You get an undefined index error because $_POST['user'] has not been set.

Use,

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>;
to see what is in the $_POST array.

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Yes, you might want to lower your error_reporting() level, and turn it off E_ALL.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Changing your error reporting means that the error will still be occuring and the script still won't be working as you expect but you won't see an error message. I'm not sure how useful that is, having the highest level of error reporting means that misspelled variable names and one's that don't exist are highlighted so that you can debug more effectively.

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

I know. I do it at work, but at the same time, the warnings it pops up aren't necessarily wrong, as long as I expect that sometimes $_POST['username'] won't be there. If my program understands that, then there is no problem.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Maybe there should be a sticky about errors Jason since the latest versions of PHP have error reporting high as default and lots of people seem to be worried about the notices they're getting.

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Ahh! I didn't know that. =) I don't have the newest version yet, I will look into it.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Having just thought about it for a second (and checking my php.ini-dist) I think it's affecting people who use the Windows self-installer to install PHP or use the php.ini-recommended file as their php.ini. php.ini-dist has got error reporting E_ALL & ~E_NOTICE.

Mac
cathari
Forum Newbie
Posts: 11
Joined: Mon May 13, 2002 9:51 pm

Post by cathari »

If it is inside the

Code: Select all

function somehandler()&#123;
      global $user;
&#125;
you have to declare it first as a global.

twigletmac : Yup! you are right if they installed their php with the installer all the notices and warnings are set to ON. I think they should install php manually.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

i probably am totally wrong on this...but you might try..

$HTTP_POST_VARS['var'];
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

you have to declare it first as a global.
Not if it is in the $_POST array - $_POST, $_GET, $_SERVER etc. are all autoglobals.

Good point hob_goblin - what version of PHP are you using jayymz?

Mac
Post Reply