无法从数据库获取下拉列表元素

时间:2019-02-14 11:13:16

标签: php

我想要2个下拉列表:

  1. 大学
  2. 学院

如果某人根据大学名称选择大学,则会在第二个下拉列表中向用户显示,并且大学名称和大学名称的另一件事存储在一个表中,例如

id,University_name,College_name

我们只从中获取数据

<?php
    $mysqli = new mysqli("localhost", "root", "", "hr");
    $query="SELECT DISTINCT University_Name FROM university";
    $result =$mysqli->query($query);
    $options="";
    while($row = $result->fetch_array(MYSQLI_BOTH)) {
        $University_Name = $row["University_Name"];
        $options .= "<OPTION VALUE=\"$University_Name\" name='customer_email'>".$University_Name.'</option>';
    }
?>

<div class="input-row">
    <div>&nbsp;</div>
    <label class="control-label">University Name <span style="color:red;">*</span></label>
    <div class="input-group"> <span class="input-group-addon"></span>
        <select name="university" id="university" class="form-control"  onchange="contrychange()" required>
            <option value="Select university">Select University</option>
            <?=$options?>
        </select>
       </div>
</div>

<?php
    $mysqli = new mysqli("localhost", "root", "", "hr");
    $query="SELECT College_Name FROM university where University_Name='$University_Name'";
    $result =$mysqli->query($query);
    $options="";
    while($row = $result->fetch_array(MYSQLI_BOTH)) {
        $College_Name = $row["College_Name"];
        $options .= "<OPTION VALUE=\"$College_Name\" name='customer_email'>".$College_Name.'</option>';
    }
?>

<div class="input-row">
    <div>&nbsp;</div>
    <label class="control-label" for="field_12">College Name<span style="color:red;">*</span></label>
    <div class="input-group"> <span class="input-group-addon"></span>
        <select name="college" id="college" class="form-control" required>
            <option value="Select college">Select College</option>
            <?=$options?>
        </select>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

如果在加载页面时填充了下拉列表,只需将foreach循环放在select标记之间。

<?php
$mysqli = new mysqli("localhost", "root", "", "hr");
$query="SELECT DISTINCT University_Name FROM university";
$result =$mysqli->query($query);
?>

<div class="input-row">
<div>&nbsp;</div>
<label class="control-label">University Name <span style="color:red;">*</span></label>
<div class="input-group"> <span class="input-group-addon"></span>
    <select name="university" id="university" class="form-control"  onchange="contrychange()" required>
        <option value="Select university">Select University</option>
         <?php
         while($row = $result->fetch_array(MYSQLI_BOTH)) {
               $University_Name = $row["University_Name"];
               echo "<option value=\"$University_Name\" name='customer_email'>".$University_Name."</option>";
         }
         ?>
    </select>
   </div>

在大学下拉菜单中也执行此步骤。很简单

答案 1 :(得分:0)

像这样在循环内更改此行 $options .= "<OPTION VALUE=".$College_Name." name=\"customer_email\">".$College_Name."</option>";