Jump to content

[SOLVED] Undefined index


papillonstudios

Recommended Posts

I am using a switch statement on my my site but when i declare the variable "action" to use with the switch statement and use the $_GET method to retrieve it.

 

Heres my error

Notice: Undefined index: action in /Users/GamingFusion/Documents/Theatre311/config.php on line 49

 

Heres my code

$action = secure($_GET['action']);

 

secure(); is a function i made

 

whats wrong? if you need anymore info or code i will post it.

Link to comment
Share on other sites

You're getting this because there is no index of action in the super-global $_GET. More specifically, there is no 'action' variable defined in your query string.

 

file.php // Error

file.php?action=something // No error

 

Edit: Solution: Check to make sure it's set before assigning that value to $action"

 

if(isset($_GET['action']))
{
     $action = secure($_GET['action']);
}

Link to comment
Share on other sites

If you have a variable that optionally might or might not exist at the time your code is executed, you need to use isset() to test if it does exist. It is common to use code like the following to give such an optional variable the actual value or a default value so your code will behave in a predictable manor when the variable does not exist -

 

$action = isset($_GET['action']) ? secure($_GET['action']) : ''; // pick a default value (and empty string in this example) that makes sense in your application

Link to comment
Share on other sites

ok thanks guys for the replys. I ahve been going through the script because it was working before thing is messed up

on everypage i get these errors

 

Notice: Undefined index: dldesc in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 58

 

Notice: Undefined index: username in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 62

 

Notice: Undefined index: password in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 63

 

Notice: Use of undefined constant guest - assumed 'guest' in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 91

 

Notice: Use of undefined constant guest - assumed 'guest' in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 92

 

Notice: Undefined index: action in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 99

 

Notice: Undefined index: sa in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 100

 

Notice: Undefined index: ssa in /Users/GamingFusion/Desktop/Isus 2.0/config.php on line 101

 

Notice: Undefined index: action in /Users/GamingFusion/Desktop/Isus 2.0/index.php on line 27

 

and for the login page for example

Notice: Use of undefined constant text - assumed 'text' in /Users/GamingFusion/Desktop/Isus 2.0/login.php on line 22

 

Notice: Use of undefined constant username - assumed 'username' in /Users/GamingFusion/Desktop/Isus 2.0/login.php on line 22

 

Notice: Use of undefined constant password - assumed 'password' in /Users/GamingFusion/Desktop/Isus 2.0/login.php on line 23

 

Notice: Use of undefined constant password - assumed 'password' in /Users/GamingFusion/Desktop/Isus 2.0/login.php on line 23

 

and i havent changed a thing but the one online works fine

 

http://isus.gamingfusion.net/demo

Link to comment
Share on other sites

It's because you have different configurations in your php.ini files, one to probably hide all the warning/notices and the other to show all. Undefined index is when you try to call a value from an array, who's key does not exist in the array.

 

$array = array('hi' => 'hello', 'test' => 'testing', 'foo' => 'bar');
echo $array['foo']; // works, because foo is a valid key
echo $array['joebob']; // will throw a notice, because we never defined the key 'joebob'
echo $array['hi']; // works, again because we defined it.

 

As for: Notice: Use of undefined constant, its when you don't use quotes around the key/index name. Without the quotes its considered a constant, and PHP then looks for the constant, and when it doesn't find it, it is smart enough to realize you meant it with quotes.

 

See this example below, the first is correct, the second looks for a constant named foo first.

$array = array('hi' => 'hello', 'test' => 'testing', 'foo' => 'bar');
echo $array['foo']; // right
echo $array[foo]; // wrong

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.