loganb Posted April 17, 2007 Share Posted April 17, 2007 I'm working on a database project for a class and am having problems populating select boxes with a field from a mysql table. The first thing that I'm trying to do is pull the field LastName from the table Faculty. Then when the user selects the faculty member in the drop down box, it will then display all of the fields related to that faculty member...first and last name, title, office number etc and allow the user to edit those fields and then resubmit them to the database. However I can't seem to get the drop down box to populate, so if anyone has any suggestions as to how to fix this I would love to hear them. The page I'm working on is http://www2.imse.ksu.edu/~loganb/HW5/facultyedit.htm and the php code I'm having problems with is as follows. <!--php connection--> <?PHP // open a mysql connection $db=mysql_connect('host', 'user', 'pass'); if (!$db) { echo 'Error: Could not connect to database. Please try again later.'; exit; } //define the database used mysql_select_db('ABET_Course_Reports'); ?> And the code for the select box <!--start select box--> <select name="Faculty"> <option>Please select a faculty member from the list</option> <? $query = "SELECT LastName FROM Faculty"; $query = mysql_query($query); $query_row=mysql_num_rows($query); while ($row=mysql_fetch_array($query)) { echo "<option>".$row['LastName']."</option>"; } ?> </select> </form> Any ideas or help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/47465-solved-dynamic-drop-down-boxes-with-mysql/ Share on other sites More sharing options...
trq Posted April 17, 2007 Share Posted April 17, 2007 You never check your query actually succeeds before trying to use it (don't let that become a habbit). Try... <?php $query = "SELECT LastName FROM Faculty"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<option>".$row['LastName']."</option>"; } } else { echo "No results found"; } } else { echo "Query failed<br />$query<br />".mysql_error(); } ?> and let us know what the output is. Quote Link to comment https://forums.phpfreaks.com/topic/47465-solved-dynamic-drop-down-boxes-with-mysql/#findComment-231634 Share on other sites More sharing options...
loganb Posted April 17, 2007 Author Share Posted April 17, 2007 Ok well I've made the change that Thorpe suggested and it does not change the output, however I think I have some other problem as well. After I changed that code with what was suggested I checked the source code through the browser and my php code is being displayed in both firefox and in explorer. My other pages that are connecting to the database and working correctly are not doing this, so I'm unsure why the php is showing up when its not supposed to. I removed the host name, login and password because of this, but if you have an idea as to why this is happening I would love to hear it. The site address is on the first post, Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/47465-solved-dynamic-drop-down-boxes-with-mysql/#findComment-231678 Share on other sites More sharing options...
trq Posted April 17, 2007 Share Posted April 17, 2007 Its a htm file, it needs to be php if you want to parse php in it. Also, use the full <?php tags, not <? Quote Link to comment https://forums.phpfreaks.com/topic/47465-solved-dynamic-drop-down-boxes-with-mysql/#findComment-231684 Share on other sites More sharing options...
loganb Posted April 17, 2007 Author Share Posted April 17, 2007 that did it, as you could guess I'm still pretty new at php programming, but it works like I was wanting. Thanks thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/47465-solved-dynamic-drop-down-boxes-with-mysql/#findComment-231687 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.