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
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 );

Link to comment
Share on other sites

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>';   

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

The @ error suppressor is never the 'right' way to handle an error. The 'right' way to do this is

 

(isset($_POST['name_list']) && $value['name'] == $_POST['name_list'] ? 'selected="selected"' : '')

 

Check if the variable exists before trying to use it.

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.