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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.