Jump to content

Display db data in drop down; insert selection into new table?


eanderson

Recommended Posts

I have searched pages of this forum and can't come up with an answer that's been given that I feel 100% relates to my problem--

 

I tried posting for help in the mySQL forum, but maybe here is where I need help. I have various different tables -- two notably, instructors and classes.

 

My problem I'm having is how to associate data from the instructors table to the classes table. I don't know if Foreign Keys are the answer, the partial answer, or not an answer at all. I can't seem to handle a good grasp on them.

 

Anyway-- I am trying to give users a drop down menu that lists all of the instructors' names from the instructors table, and have them select one to be submitted in a form which should be saved in a new table (the classes table) to associate each class with an instructor.

 

Here is the code I have for my form selection:

 

 

Instructor: <select name="instructorid">
<option value="">--</option>
<?php
$query = mysqli_query("SELECT * FROM instructors ORDER BY instructor_first ASC");
$result = mysqli_query($query) or die (mysqli_error());
while ($r = mysqli_fetch_assoc($result)) {
  echo "<option value=".$r['instructor_id'].">".$r['instructor_name']."</option>";
}
?>
</select>

 

Now, what would be the best way of inserting this information into the database to save the selection so it can be a reference to the class table?

 

Would I create a Foreign Key called instructor_id in the classes table? Is there a better way to do this? I want to eventually be able to pull up the class information to display and instead of it just showing the instructor_id, I want it to be able to display the other instructor information if necessary.

 

I hope this makes sense. Thank you for looking.

I think you have the right idea here - use instructor_id on the classes table. Later on, you can always use a simple mysql join query to get the instructor's information where instructor_id=instructor_id ...

 

:) Does that make sense?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.