我创建了一个html表单。在那里我有2个下拉列表。下拉列表1从sql数据库表中检索值并在那里显示。现在我想显示另一个下拉列表,其中包含来自下拉列表1的输入。
这是我的代码:
<label for="bname">Select a Building</label>
<?php
session_start();
include 'db_connection.php';
$conn = OpenCon();
$sql = "SELECT bname FROM building_details";
$result = mysqli_query($conn, $sql);
echo "<select name='bname'>";
while ($row = mysqli_fetch_array($result)){
echo "<option value='". $row['bname'] ."'>".$row['bname'] ."</option>";
}
echo "</select>";
echo "<label for='rtype'>Select a rtype</label>";
$sql2 = "SELECT rtype FROM room_details WHERE bname='bname'";
$result2 = mysqli_query($conn,$sql2);
echo "<select name='rtype'>";
while ($row2 = mysqli_fetch_array($result2)){
echo "<option value='". $row2['rtype'] ."'>".$row2['rtype'] ."</option>";
}
echo "</select>";
这里一旦bname值从building_details被选中然后根据该输入我需要显示另一个下拉列表,它是来自room_details表的rtype列。 有人可以帮我这个???
答案 0 :(得分:0)
mysqli_connect_errno() - 返回上次连接调用的错误代码
mysqli_connect_error() - 返回上次连接错误的字符串描述
mysqli_errno() - 返回最近函数调用的错误代码
mysqli_sqlstate() - 返回先前MySQL操作的SQLSTATE错误
如需更多帮助,请使用以下链接:http://php.net/manual/en/mysqli.error.php
答案 1 :(得分:0)
这是我发现的唯一方法。 我希望这就是你要找的东西!
<label for="bname">Select a Building</label>
<?php
session_start();
include 'db_connection.php';
$conn = OpenCon();
$sql = "SELECT bname FROM building_details";
$result = mysqli_query($conn, $sql);
$bnames = "";
echo "<select name='bname'>";
while ($row = mysqli_fetch_array($result)){
echo "<option value='". $row['bname'] ."'>".$row['bname'] ."</option>";
$bnames .= $row['bname'] . ", ";
}
$bnames = substr($bnames, 0, -2);
echo "</select>";
echo "<label for='rtype'>Select a rtype</label>";
$sql2 = "SELECT rtype FROM room_details WHERE bname IN($bnames)";
$result2 = mysqli_query($conn,$sql2);
echo "<select name='rtype'>";
while ($row2 = mysqli_fetch_array($result2)){
echo "<option value='". $row2['rtype'] ."'>".$row2['rtype'] ."</option>";
}
echo "</select>";
如果需要避免sql注入,请不要忘记使用htmlspecialchars函数转义$bnames
变量。