adzie Posted October 27, 2007 Share Posted October 27, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/ Share on other sites More sharing options...
rajivgonsalves Posted October 27, 2007 Share Posted October 27, 2007 well in fetch array you should give the column name not the index if($row['3'] == $_GET['Country']['0']) $row['column_name'] Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379332 Share on other sites More sharing options...
GingerRobot Posted October 27, 2007 Share Posted October 27, 2007 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']) Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379344 Share on other sites More sharing options...
marcus Posted October 27, 2007 Share Posted October 27, 2007 Yep, what Ginger said. There are three ways to produce a result from mysql fetch array, MYSQL_NUM, MYSQL_ASSOC and MYSQL_BOTH. Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379347 Share on other sites More sharing options...
adzie Posted October 27, 2007 Author Share Posted October 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379352 Share on other sites More sharing options...
GingerRobot Posted October 27, 2007 Share Posted October 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379375 Share on other sites More sharing options...
adzie Posted October 27, 2007 Author Share Posted October 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379396 Share on other sites More sharing options...
thebadbad Posted October 27, 2007 Share Posted October 27, 2007 In valid XHTML it should be selected="selected" instead of just selected. Maybe that will solve it. Remember that in XHTML, all tags must be closed and every attribute must have a value (enclosed in double quotes). Quote Link to comment https://forums.phpfreaks.com/topic/75009-drop-down-options/#findComment-379493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.