Jump to content

Undefine index issue


Shadowing

Recommended Posts

hey guys having troubles figuring out my problem here

Just a standard populating drop down box

I must be over looking something here.

I should have two values from the away which I do

print_r checks out fine on $mods. so nothing wrong with the array.

 

Notice: Undefined index: name_list

When I see this error I think name_list isnt defined. Only one of the two names display in the drop down box the other one displays out side of it. The error started after adding a 2nd name to the drop down box.

 

$result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error());
$mods = array();

while($raw = mysql_fetch_assoc( $result )) {    

	$mods[] = $raw;
}

	echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">';


foreach($mods as $key => $value) {        

	echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST
['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options></select></td>';

 

 

Link to comment
https://forums.phpfreaks.com/topic/262491-undefine-index-issue/
Share on other sites

hey guys having troubles figuring out my problem here

Just a standard populating drop down box

I must be over looking something here.

I should have two values from the away which I do

print_r checks out fine on $mods. so nothing wrong with the array.

 

Notice: Undefined index: name_list

When I see this error I think name_list isnt defined. Only one of the two names display in the drop down box the other one displays out side of it. The error started after adding a 2nd name to the drop down box.

 

$result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error());
$mods = array();

while($raw = mysql_fetch_assoc( $result )) {    

	$mods[] = $raw;
}

	echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">';


foreach($mods as $key => $value) {        

	echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST
['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options></select></td>';

 

...

print_r( $_POST );

here is the new code now after the change to the foreach bracket

$result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error());
	$mods = array();

while($raw = mysql_fetch_assoc( $result )) {    

		$mods[] = $raw;
}

	echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">';


foreach($mods as $key => $value) {        

	echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>';

	 } 
	echo '</select></td><td class="submit"><input type="submit" name="remove" id="remove" value="Remove"></td>';   

thanks xyph that fixed the problem of the 2nd name not being in the drop down box.

 

the error notice still exist though. hmm

I did print_r($_POST['name_list']); and got the same error

 

It's because you haven't posted the form yet, so the data for name_list does not exist in the php $_POST array. Therefore you are getting an error telling you that the index(['name_list']) does not exist in the array. It's not going to mess up execution of your script and those errors should not be displayed anyway in a production environment. But for now you can suppress it like so:

 

$result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error());
	$mods = array();

while($raw = mysql_fetch_assoc( $result )) {    

		$mods[] = $raw;
}

	echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">';


foreach($mods as $key => $value) {        

	echo '<option value="' . $value['name'] . '" ' . ($value['name'] == @$_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>';

	 } 
	echo '</select></td><td class="submit"><input type="submit" name="remove" id="remove" value="Remove"></td>';

wierd i dont have this issue with other drop down boxes.

Im not a big fan of using @

I over looked the bracket being before the end select tag when comparing it to the other drop down boxes i have.

but the other ones i have are exactly the same now

 

some times if i do have a undefine variable i'll do

 

$name_list = !isset($name_list) ? '' : $name_list;

 

hmm

wierd i dont have this issue with other drop down boxes.

Im not a big fan of using @

I over looked the bracket being before the end select tag when comparing it to the other drop down boxes i have.

but the other ones i have are exactly the same now

 

some times if i do have a undefine variable i'll do

 

$name_list = !isset($name_list) ? '' : $name_list;

 

hmm

 

I generally use the @ to save bytes, especially for something as simple as checking the value of a variable that may or may not exist. I also understand how you feel about not wanting to suppress errors, should they exist. Just a personal preference really.

 

That's weird that the error is not showing up when it's only populating from 1 result, it doesn't seem like that should make any difference in the post array.

I see whats going on now

 

For starters i coded a long time with out using all errors, which is bad cause its real good for seeing mistakes.

 

my other drop down boxes are using a php variable in place of name_list on the orginal example.

echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $current['name_list'] ? 'selected="selected"' : '')

I see what you are saying now how the variable doesnt exist cause the html doesnt exist

i dont understand why it would matter if im displaying one result or more. what the heck lol

 

thanks alot for the help. much appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.