rvinikof Posted July 16, 2007 Share Posted July 16, 2007 I'm trying to input data from a field called subject to insert the different subjects into a drop down on a form to be selected. When I do this: $sqlsubject = "SELECT DISTINCT subject FROM courseinfo"; echo '<b>Subject:</b><select name = "subject"><option value = "subject">$sqlsubject</option></select>'; I just get the drop down saying $sqlsubject, instead of the list of subjects from the database. Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/ Share on other sites More sharing options...
Wildbug Posted July 16, 2007 Share Posted July 16, 2007 There are (at least) two reasons for this. #1. You're quoting a variable in single quotes; there will be no interpolation. See strings. #2. You haven't done anything with the query string. Study the examples under MySQL functions. Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-299720 Share on other sites More sharing options...
rvinikof Posted July 16, 2007 Author Share Posted July 16, 2007 You were right obviously. But now I've done this: $sqlsubject = "SELECT DISTINCT subject FROM courseinfo"; $subjectquery = mysql_query($sqlsubject) or die('Query failed: ' . mysql_error()); while ($subjectrow = mysql_fetch_row($subjectquery)) { echo '<b>Subject:</b><select name = "subject"><option value = "subject">'; foreach ($subjectrow as $subject) { echo $subjectrow; } echo "</option></select>"; } and the drop down just says Array. What does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-299849 Share on other sites More sharing options...
paul2463 Posted July 16, 2007 Share Posted July 16, 2007 try this $sqlsubject = "SELECT DISTINCT subject FROM courseinfo"; $subjectquery = mysql_query($sqlsubject) or die('Query failed: ' . mysql_error()); //start the select box off echo 'Subject:<select name = "subject">'; //create the select box items from the query while($row = mysql_fetch_assoc($subjectquery)) { $subject = $row['subject']; echo "<option value = '$subject'>$subject</option>"; } //close the select box echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-299865 Share on other sites More sharing options...
Wildbug Posted July 16, 2007 Share Posted July 16, 2007 ....and the drop down just says Array. What does that mean? Paul's got the right solution. mysql_fetch_row() returns an array, so instead of echoing the array, you need to echo the element(s) of the array you really want. mysq_fetch_row() returns a numerically indexed array; mysql_fetch_assoc() returns an array indexed by the column names. Oh, and in a foreach($foo as $bar) {...} loop, you refer to $bar, not $foo within the loop. Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-299917 Share on other sites More sharing options...
rvinikof Posted July 17, 2007 Author Share Posted July 17, 2007 Thanks. One last question... once I have the data in the drop down, is there a way I can put a blank field before all the data (at index 0) so that it will be blank instead of data from the table appearing? Can I do this without having to add null values or something like that in my table? Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-299975 Share on other sites More sharing options...
Wildbug Posted July 17, 2007 Share Posted July 17, 2007 Well, if you add a row to your table and don't have any value to put in a particular field, then it will have to be NULL or 0 or the empty string (""). In the code that inserts data you should also have some data validation (check that numeric data really is numeric, VARCHAR(255) is really =< 255 chars, etc), and you can set blank fields in that code. Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-299993 Share on other sites More sharing options...
Illusion Posted July 17, 2007 Share Posted July 17, 2007 well, u can't do that with DO...WHILE loop instead of WHILE loop with $subject="" declared before the do...while loop. Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-300215 Share on other sites More sharing options...
Wildbug Posted July 17, 2007 Share Posted July 17, 2007 Or just echo it before the loop Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-300336 Share on other sites More sharing options...
paul2463 Posted July 17, 2007 Share Posted July 17, 2007 as wildbug said, $sqlsubject = "SELECT DISTINCT subject FROM courseinfo"; $subjectquery = mysql_query($sqlsubject) or die('Query failed: ' . mysql_error()); //start the select box off echo 'Subject:<select name = "subject">'; //put in the first select part saying please select echo "<option value = ''>Please Select</option>"; //create the select box items from the query while($row = mysql_fetch_assoc($subjectquery)) { $subject = $row['subject']; echo "<option value = '$subject'>$subject</option>"; } //close the select box echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/#findComment-300483 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.