在HTML页面中从MySQL下拉列表

时间:2012-04-10 20:08:50

标签: php html drop-down-menu

我试图在html页面中放置一个从mysql生成的下拉列表,事情是这个代码在php页面中工作正常,但它不在html页面中,我把它放在php标签中但没有显示,我将不胜感激任何帮助 这是我的代码

<!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>

3 个答案:

答案 0 :(得分:1)

我个人不喜欢在PHP中回显html所以我使用这种方法,无论如何你的问题是基于HTML而不是基于php ...

查询DB的示例PHP模型:

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

用于处理DOM和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>

简而言之,您在此处遇到错误: echo " <select name='courseID' >Course</option>";

应该是: echo " <select name='courseID' >Course</select>";


此外,您无需设置$curvalue = $curvalue+1;您只需$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++;
 }

另请注意 其最佳做法是不将DOM与服务器端代码混合使用。 IE你应该把while语句和选择框放在一个单独的php视图文件中,该文件迭代事物,而不是使用echo。这样就减少了服务器处理和客户端处理。

答案 1 :(得分:1)

我认为这个脚本是你想要做的:

<!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>\n";

            $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>\n";

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

您的脚本名称应以PHP扩展名结尾,例如.php或.phtml。

答案 2 :(得分:0)

也许是因为你这样做:

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

将其更改为:

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