Jump to content

Recommended Posts

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.

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.

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.

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.