在HTML页面中从MySQL下拉列表

im trying to put a drop down list generated from mysql in html page , the thing is this code work fine in a php page but it doesnt in html page , I've put it in a php tags yet nothing was shown , I would appreciate any help here is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Add Qustions</title>
 </head>
 <body>
  <form action="saveQ.php" method="post" >

  <br />
  <?php   $connectdb = mysql_connect('localhost','root','sara') or die ("Not Connect");
  if (!$connectdb)
  {
      die('Could not connect :'. mysql_errno());
  }
  $selestdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
   $qu = mysql_query("SELECT ID,Name FROM Course ORDER BY ID asc") or die ("mysql error");
    echo "Choose the course that you want to create the test for : <br /> ";
    echo " <select name='courseID' >Course</option>";
    echo "<option value=0>Course </option>";
     $curvalue=2;
    while  ($row = mysql_fetch_assoc($qu)){
    echo '<option value="' .$row[ID].'">' .$row[ID].' ' .$row[Name].'</option>';
    $curvalue = $curvalue+1;
     }
     echo "</select> "; ?>
     <br />
      /// some other unrelated code here 
    <input type="submit" value="Save Question" />
    </form>

    </body>
    </html>

Maybe because you are doing it like this:

echo " <select name='courseID' >Course</option>";

Change it to:

echo "<select name='courseID'>";

I personally don't like to echo out html in PHP so i use this method, in any case your issue is html based and not php based...

Example PHP Model to Query DB :

<?php
$selectdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
$qu = mysql_query("SELECT ID,Name FROM Course ORDER BY ID asc") or die ("mysql error");
?>

Example View File to Handle the DOM and html.

<p>Choose the course that you want to create the test for:</p><br />
<select name="courseID">
    <option value="0">0 Course</option>
    <option value="1">1 Course</option>
  <?php while( $row = mysql_fetch_assoc($qu) ): ?>
    <option value="<?= $row['ID']; ?>"><?php echo $row['ID'].' '.$row['Name']; ?></option>
  <?php endwhile; ?>
</select>

In short, you have an error here: echo " <select name='courseID' >Course</option>";.

It should be: echo " <select name='courseID' >Course</select>";


Also, you dont need to set $curvalue = $curvalue+1; You can simply do $curvalue++; IE:

<?php
  while  ($row = mysql_fetch_assoc($qu)){
  <select name="courseID">
  <option value="<?= $row['ID']; ?>"><?php echo $row['ID'].' '.$row['Name']; ?></option>
  </select>
   $curvalue++;
 }

Also note that its considered best practice to not mix DOM with Server side code. IE you should put the while statement and select boxes in a seperate php view file that iterates through things, rather than using echo. This way there is less server processing and more client side processing.

I think this script is what you wanted to do:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Add Qustions</title>
</head>
<body>
    <form action="saveQ.php" method="post">
        <br />
        <?php  
            $connectdb = mysql_connect('localhost','root','sara') or die ("Not Connect");
            if (!$connectdb) die('Could not connect :'. mysql_errno());

            echo "          Choose the course that you want to create the test for: <br />
            <select name=\"courseID\">
                <option value=\"0\">Course </option>
";

            $selestdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
            $qu = mysql_query("SELECT ID,Name FROM Course ORDER BY ID asc") or die ("mysql error");
            while ($row = mysql_fetch_assoc($qu))
                echo "              <option value=\"{$row["ID"]}\">{$row["ID"]} {$row["Name"]}</option>
";

            echo "          </select> ";
        ?>
        <br />
        <!-- some other unrelated code here -->
        <input type="submit" value="Save Question" />
    </form>
</body>
</html>

Your script's name should end with a PHP extension, like .php or .phtml.