Jump to content

drop down options


adzie

Recommended Posts

Hi,

 

I've got a drop down box which when I select the relevant item correcly updates the database but when I return to edit the form again the drop down is showing the wrong option even though the correct option is in the DB

 

any thoughts?

 

<td><select name=\"country\">";
				$country = "select * from country";
				$country = mysql_query($country,$conn);
				while($country1 = mysql_fetch_array($country))
				{
					$msg.="<option ";
					if($row['3'] == $_GET['Country']['0'])
					{
						$msg.="selected";
					}
					$msg.=" value=".$country1['0'].">".$country1['1']."</option>";
				}
				$msg.="<tr/>
<td>

Link to comment
Share on other sites

well in fetch array you should give the column name not the index

 

 

if($row['3'] == $_GET['Country']['0'])

 

$row['column_name']

 

Not true. mysql_fetch_array, when used without any parameters, returns BOTH an associative and a numerically indexed array. It's perfectly valid to use numerical indexes, even if it does make your code a little unreadable.

 

Your issue appears to be that you are assigning the returned array from the mysql_fetch_array function to a different variable, $country1. Try:

 

 

if($country1['3'] == $_GET['Country']['0'])

 

 

Link to comment
Share on other sites

thanks I've amended as suggested but no result as yet.

 

the record displays correctly using this

 

<td><input type=\"text\" name=\"country\" value=\"$_GET[country]\" size=\"50\"/></td>

 

<td><select name=\"country\">";

$country = "select * from country";

$country = mysql_query($country,$conn);

while($country1 = mysql_fetch_array($country))

{

$msg.="<option ";

if($row['3'] == $_GET['Country']['0'])

{

$msg.="selected";

}

$msg.=" value=".$country1['0'].">".$country1['1']."</option>";

}

$msg.="<tr/>

<td>

 

 

the drop down shows the records from the team table and if I select a record it updates the database correctly but when I enter the form again the record is correct but the drop down shows the first option, not the correct one if that makes any sense.

Link to comment
Share on other sites

Im confused by your code. Why do you have a text box and a select box that are both using the $_GET value and both have the same name? One (i would assume the later, so the select box) will overwrite the other.

 

Second, did you actually make the changes? I still see $row['3'] rather than $country1[3]

 

Third, in this line:

if($row['3'] == $_GET['Country']['0'])

 

You've used Country with a capital C - both of your form elements use a lower case c. Array keys are case sensitive.

Link to comment
Share on other sites

I have made the changes as changed the Country to country as suggested.

 

its weird it updates the database as the text field shows the correct result but the drop down box just shows the first option not the correct one, I dont want the text field but with it there it shows the database is being updated for testing

 

 

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.