iceblox Posted October 30, 2007 Share Posted October 30, 2007 Hi Guys how would i make a PHP Query select the one that has been selected in a drop down box? This is query i tried which didnt work Can anyone please tell me what is wrong with it? $query = "SELECT * FROM deals WHERE FreeGift <> 'Offer.FreeGift' AND FreeGift <> ' 'GROUP BY FreeGift ASC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) if($row[4] == '$FreeGift') { $auto = '<option value="' . $row[4] . '">' . $row[4] . '</option>'; } else { $auto = '<option value="' . $row[4] . '">' . $row[4] . '</option>'; } echo '' . $auto . ''; } else { echo ''; } Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/ Share on other sites More sharing options...
sdi126 Posted October 30, 2007 Share Posted October 30, 2007 You are missing the keyword "selected"...both of your if statements are the same. It should be if($row[4] == '$FreeGift') { $auto = '<option selected="selected" value="' . $row[4] . '">' . $row[4] . '</option>'; } Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381370 Share on other sites More sharing options...
iceblox Posted October 30, 2007 Author Share Posted October 30, 2007 Ooops lol The only issue is that when i have that if statement it only brings up one option when there should be about 20? Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381379 Share on other sites More sharing options...
sdi126 Posted October 30, 2007 Share Posted October 30, 2007 Use mysql_fetch_array instead of row while($row = mysql_fetch_array($result)) Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381381 Share on other sites More sharing options...
iceblox Posted October 30, 2007 Author Share Posted October 30, 2007 Whats the difference? Sorry to be stupid Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381387 Share on other sites More sharing options...
sdi126 Posted October 30, 2007 Share Posted October 30, 2007 It just returns a reference to one row. Using array you have access to all the rows returned by the sql statement. Also it is better practice to refer to the variable by name and not by array number....ie $row[4] should be something like $row["freegift"] if your field name was called freegift of course in the table Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381396 Share on other sites More sharing options...
iceblox Posted October 30, 2007 Author Share Posted October 30, 2007 Hmm that is still allowing returning one row.. Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381403 Share on other sites More sharing options...
sdi126 Posted October 30, 2007 Share Posted October 30, 2007 Echo out the number of rows returned by your sql statement and make sure it is more than one. Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381440 Share on other sites More sharing options...
iceblox Posted October 30, 2007 Author Share Posted October 30, 2007 OK thanks for the help ill look into it Link to comment https://forums.phpfreaks.com/topic/75394-drop-down-boxes-selecting-selected-option/#findComment-381446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.