0

我试图在 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>
4

3 回答 3

1

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.

于 2012-04-10T20:16:27.830 回答
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。

于 2012-04-10T20:25:30.883 回答
0

也许是因为你这样做:

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

将其更改为:

echo "<select name='courseID'>";
于 2012-04-10T20:12:06.263 回答