下拉“选择”列表不会从mysql数据库查询中填充。我的错误在哪里?

时间:2014-06-17 02:11:49

标签: php mysql

为什么我的下拉列表没有填充?我希望有人可以指出我的错误。

我的页面呈现正确,我看到下拉列表,但我只包含一个条目,即"选择一个事件"。

我哪里错了?

当前代码:

<form class="form" action="http://host/ants/connie/addExhibit.php" method="post">
   <fieldset>
      <legend>Add New Exhibit Record:</legend>
              Choose Event :
              <select name="event">
                     <option value="" selected>select an event</option>
                     <?php
                     $con=mysqli_connect("server","root","pw","db");
                     //check connection
                     if (mysqli_connect_error())
                             {
                             echo "Failed to connect to MySQL: " . mysqli_connect_error();
                             }
                     $sql="select id, name from event order by name";
                     $result = mysqli_query($con,"$sql");
                     while($row = mysqli_fetch_array($result))
                             {
                     echo "<option value = \"" . $row['id'] . "\">" . $row['name'] . "</option>"
                     ?>
                     <?php
                     }
                     ?>
             </select><br>
              Choose Exhibitor :      <input type="text" size="30" name="exhibitor" /><br>
              Exhibit Name:           <input type="text" size="30" name="venue" /><br>
              Price:                  <input type="text" size="30" name="convenor" /><br>
              Sold:                   <input type="text" size="30" name="email" /><br>
      <input type="submit">
   </fieldset>
</form>

1 个答案:

答案 0 :(得分:0)

这里使用mysql_connect可以正常工作,你也可以用mysqli_connect方式完成。

<?php
$connect=new connect("server","root","pw","db");
class connect{
     function __construct($host,$user,$password,$db_name){
        mysql_connect($host,$user,$password) or die("Connection error");
        mysql_select_db($db_name);
        $error=mysql_error();
        if (!empty($error))
        {
            echo $error;
        }
    }
}
$query=mysql_query("SELECT id, name FROM event ORDER BY name");         
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Check Dropdown</title>
</head>
<body>
<form >

            <fieldset>
              <legend>Get the drop down Value </legend>

              <select class="form-control input-sm" name="branch">
                    <option value="">Select</option>

                     <?php 
                      while($row=mysql_fetch_array($query))
                        {
                            echo '<option value="'.$row["id"].'">'.$row["name"].'</option>';
                        }
                    ?>
                  </select>
               </fieldset>
          </form>

</body>
</html>