Jump to content

Trying to get form to display table content on drop down


Johnnyboy123

Recommended Posts

Name says it all. I have a form and 2 tables that are going to have to be linked with each other. Although what I'm trying to do now is very basic and I'm struggling haha Though this is my first php project :)

 

Basicly I got a form with a drop down menu, which I want to display the contents of my table (course) which the user then selects from and submits to another table (student)

 

Here is my code for this part

<?php

<?php
$q = "SELECT cname FROM course ";
$result = mysql_query($q);
$course = mysql_fetch_array($result);

?>
<div id="apdiv3">

<FORM action = "demo.php" method ="post">

<p>Course name:</p>
<select name="cname"> <OPTION> <?php echo $course['cname']; ?> </option> </SELECT>

?>

 

It only displays 1 item from my course table. What am I doing wrong? And also please explain so I can learn from my mistakes ;)

You are only selecting the top level result that is returned in the array.  You will need to run through the array in order to get what you want out.

<div id="apdiv3">

<FORM action = "demo.php" method ="post">

<p>Course name:</p>
<select name="cname"> 
<?php
$q = "SELECT cname FROM course ";
$result = mysql_query($q);
WHILE ($course = mysql_fetch_array($result)){
echo "<OPTION> value='{$course['cname']}'>{$cource['cname']}</option>";
      }
echo " </SELECT>"

 

Should do it.  You also need to look into how to form <option>'s better.

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.