Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Undefined Index Error - how do I avoid this?

    I am getting the following error:

    Notice: Undefined index: message in /data/www/html/helppages/cpanel/question_level.php on line 6


    I have variables on this page that sometimes I send values into and sometimes I don't. When I don't I get this error. How can I avoid this issue. It would be very cumbersome to put a null value into a variable everytime I needed it to not have a value.

    Thank you.

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Test your values before you use them:

    PHP Code:
    if( isset($array['index']) )
    {
      
    //use $array['index']
    }
    else
    {
      
    //don't use $array['index']


  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    They aren't always arrays that I am using. They are also just plain variables holding one value. So do I have to do the isset for each variable? For example, the variables below are being declared in one of my php files that is called by a number of different pages. So one page may be sending in the levelonesave and not the leveltwosave. So the $leveltwosave = $_POST["leveltwosave"]; gives this error.

    $levelonesave = $_POST['levelonesave'];
    $leveltwosave = $_POST["leveltwosave"];
    $levelthreesave = $_POST["levelthreesave"];

    $levelonedelete = $_POST["levelonedelete"];
    $leveltwodelete = $_POST["leveltwodelete"];
    $levelthreedelete = $_POST["levelthreedelete"];

    $newlevelone = $_POST["newlevelone"];
    $newleveloneheading = $_POST["newleveloneheading"];
    $newleveltwo = $_POST["newleveltwo"];
    $newleveltwoheading = $_POST["newleveltwoheading"];
    $newlevelthree = $_POST["newlevelthree"];
    $newlevelthreeheading = $_POST["newlevelthreeheading"];

    $questions = $_POST["questions"];

    $heading=$_POST['heading'];
    $newheading = $_POST["newheading"];
    $description=$_POST['description'];
    $newdescription=$_POST['newdescription'];

    $one = $_POST['level1'];
    $two = $_POST['level2'];
    $three = $_POST['level3'];

    $error = 0;

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right. You need to make sure $_POST["leveltwosave"] is set before you try to put it into another variable:

    PHP Code:
    if( isset($_POST["leveltwosave"]) )
    {
      
    $leveltwosave $_POST["leveltwosave"];
    }
    else
    {
      
    $leveltwosave NULL//or '' or whatever

    The ternary operator is also nice for this sort of checking.

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay - I didn't realize that I had to do that for each variable. That's goign to make my code so much longer. Thanks for the info.

    I also have some code that is doing a count based on what is in a result set. But I am getting this same error for some reason. What am I doing wrong here?

    $query = "SELECT * FROM questions where level_one='$one' AND level_two='$two' AND level_three='$three' ORDER BY level_one ASC";
    $result = mysql_query($query) or die(mysql_error());

    $count = 1;
    $k=0;
    $line = mysql_fetch_array($result);
    for($j=0; $j<16; ++$j)
    {
    $qnumber="question"."$j";
    if($line[$qnumber])
    {
    $count = $count+1;
    }
    }

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you don't have a question0, $j should start at 1 instead -- that may be the problem.

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Ah - thanks for your help!

  • #8
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your case, I would rather do the following:
    PHP Code:
    $levelonesave = isset($_POST['levelonesave']) ? stripslashes($_POST['levelonesave']) : "";
    $leveltwosave = isset($_POST['leveltwosave']) ? stripslashes($_POST['leveltwosave']) : "";
    $levelthreesave = isset($_POST['levelthreesave ']) ? stripslashes($_POST['levelthreesave']) : "";

    // go on 
    and include that file in every page.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •