...

View Full Version : Undefined Index Error - how do I avoid this?



JJ0117
08-23-2006, 04:59 PM
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.

Kid Charming
08-23-2006, 05:02 PM
Test your values before you use them:



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

JJ0117
08-23-2006, 05:07 PM
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;

Kid Charming
08-23-2006, 05:13 PM
Right. You need to make sure $_POST["leveltwosave"] is set before you try to put it into another variable:



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


The ternary operator (http://us3.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) is also nice for this sort of checking.

JJ0117
08-23-2006, 05:20 PM
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;
}
}

Kid Charming
08-23-2006, 05:30 PM
If you don't have a question0, $j should start at 1 instead -- that may be the problem.

JJ0117
08-23-2006, 05:46 PM
Ah - thanks for your help!

maltrecho
08-24-2006, 09:17 AM
In your case, I would rather do the following:


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum