Jump to content


Photo

Undefined Index Errors


  • Please log in to reply
3 replies to this topic

#1 mastermike707

mastermike707
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 02 August 2006 - 11:53 PM

Uh, what causes undefined index errors? Is it sql related or session related?
[url=http://www.mastermike707.com/new/][/url]

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 02 August 2006 - 11:54 PM

it means that you are referencing a variable that doesn't exist.  It should be just a warning and not an error that would cause the script to stop running.

#3 mastermike707

mastermike707
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 03 August 2006 - 12:02 AM

Can you give me a example of something that would cause this error and then the fixed version of that code.

Thank you already.
[url=http://www.mastermike707.com/new/][/url]

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 11:09 AM

A Simple example of this would be when using forms, the incorrect way:
<?php

// get the users name:
$name = $_POST['name'];

// get the user age:
$age = $_POST['age'];

// display thier name and age:
if($name != "" && $age != "")
{
    echo "Hello {$name}, you are {$age}";
    echo "<br /><br />";
}

?>
<form action="" method="post">
  Name: <input type="text" name="name" /><br />
  Age:&nbsp; <input type="text" name="age" size="3" />
  <input type="submit" value="Display Message" />
</form>
When you first run that code you'll get to undefined index messages:

Notice: Undefined index: name in C:\server\www\test.php on line 4

Notice: Undefined index: age in C:\server\www\test.php on line 7

Now the proper way:
<?php

// check that form has been submitted:
if(isset($_POST['submit']))
{
    // now that form has been submitted we can work with form variables

    $errors = '';
    
    // check that form fields has been filled in:
    if(isset($_POST['name']) && !empty($_POST['name']))
    {
        $name = $_POST['name'];
    }
    else
    {
        $errors[] = 'You have not filled in the name field';
    }

    // check the age variable is numeric
    if(isset($_POST['age']) && is_numeric($_POST['age']))
    {
        $age = $_POST['age'];
    }
    else
    {
        $errors[] = 'You have not filled in the age field correctly';
    }

    // check there are no errors:
    if(is_array($errors))
    {
        echo "Please correct the following errors:\n<ul>";

        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }

        echo "</ul>\n";
    }
    else
    {
        echo "Hello {$name}, you are {$age}";
        echo "<br /><br />";
    }
}

?>
<form action="" method="post">
  Name: <input type="text" name="name" /><br />
  Age:&nbsp; <input type="text" name="age" size="3" />
  <input type="submit" name="submit" value="Display Message" />
</form>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users