如何基于从动态下拉列表中选择选项显示相应的HTML MySQL的列(How to display c

2019-09-30 19:15发布

我想在用户选择从动态下拉列表框中选择一个选项后显示相应的MySQL表列寻求帮助。 我真的不知道我在哪里出了错:(请帮助:(

我有3个MySQL表:建筑物,delivery_transaction和位置,都相互连接。 主表是交货的交易:

[delivery_transaction表,其中building_ID和LOCATION_ID是FKS从剩余的2代表] [1]

其中,如果用户点击任何建筑物名称出现在下拉列表中,将只显示我从主表中查询如下列。

这里是我到目前为止的代码:

<form name="bldg_form" method="post" action="">

<?php

//establish sql connection with db


$con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

if(!$con)
{
  echo "Failed to connect!";
}

//select columns from delivery_transaction, buildings and location table
$query = mysqli_query($con, "SELECT delivery_status, starting_time, 
        arrival_time, duration, buildings.building_name, 
        location.location_name from delivery_transaction, buildings, 
        location where delivery_transaction.building_ID = 
        buildings.building_ID and delivery_transaction.location_ID = 
        location.location_ID");
?>
<!--Creates dropdown box-->
<select name = 'bldg'>
<option value = "">Choose Building</option>;
<?php
while($row = mysqli_fetch_assoc($query))
   {
     if($row['building_name'] == $selectedbldg)
     {
       echo '<option value = \"'.$row['building_ID'].'" 
       selected>'.$row['building_name'].'</option>';  
     }
     else
     {
       echo '<option value 
            =\"'.$row['building_ID'].'">'.$row['building_name'].'</option>';
     }
   }
 ?>
</select>
<input type="submit" name="view"/>
</form>

<section class="row text-center placeholders">
<div class="table-responsive">
<p>
 <table class="table table-striped">
  <thead>
   <tr>
     <th>Delivery Status</th>
     <th>Starting Time</th>
     <th>Arrival Time</th>
     <th>Duration</th>
     <th>Location</th>
   </tr>
</thead>
<tbody>
<tr>

<?php
if(isset($_POST['bldg']))
{
  while($row = mysqli_fetch_row($query))
  {
     echo "<tr>"."<td>".$row['delivery_status']."</td>"."
           <td>".$row['starting_time']."</td>"."
           <td>".$row['arrival_time']."</td>"."
           <td>".$row['duration']."</td>"."
           <td>".$row['location_name']."</td>"."</tr>";
   } 
 }
else
{
  echo "No results to display";
}
?>
</tr>
</tbody>
</table>
</p>
</div>
</section>
</main>

What I want to do is if user clicks on an option, it will display the 
corresponding table just like I queried. However, nothing displays :(

[This link shows user choosing an option][2]
[1]: https://i.stack.imgur.com/H78Gp.png
[2]: https://i.stack.imgur.com/pA6gI.png

Answer 1:

如果我理解正确的,你有一个下拉菜单,显示所有的建筑物。

提交后,显示根据所选择的建筑物交付的表格。

这是2个查询(你只有一个)。

这是我会怎么做:

// never a bad idea to turn on your PHP error reporting in development
error_reporting(E_ALL);
ini_set('display_errors', 1);

$con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

// always query buildings bc we need it for the dropdown
$bquery = mysqli_query($con, "SELECT building_ID, building_name FROM buildings");

$selectedbldg = null;

// if the form was submitted
if (!empty($_POST['bldg'])) {
    // store selected building_ID
    $selectedbldg = $_POST['bldg'];
    // query deliveries based on building; 
    // note the additional condition (I'm assuming building_ID is an integer)
    $dquery = mysqli_query($con, "
        SELECT  delivery_status, starting_time, arrival_time, duration, buildings.building_name, 
                location.location_name
        FROM    delivery_transaction, buildings, location 
        WHERE   delivery_transaction.building_ID = buildings.building_ID
        AND     delivery_transaction.location_ID = location.location_ID
        AND     buildings.building_ID = {$selectedbldg}
    ");

    // though it is not part of the scope of your question, 
    // you should probably use prepared statement above
}
?>


<form name="bldg_form" method="post" action="">
    <select name="bldg">
        <option value="">Choose Building</option>;
        <?php while ($row = mysqli_fetch_assoc($bquery)) : ?>
            <option value="<?= $row['building_ID'] ?>" <?= $row['building_name'] == $selectedbldg ? 'selected' : '' ?>><?= $row['building_name'] ?></option>
        <?php endwhile ?>
    </select>
    <input type="submit" name="view" />
</form>


<section class="row text-center placeholders">
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Delivery Status</th>
                    <th>Starting Time</th>
                    <th>Arrival Time</th>
                    <th>Duration</th>
                    <th>Location</th>
                </tr>
            </thead>
            <tbody>
            <?php if (isset($dquery) && mysqli_num_rows($dquery)) : ?>
                <?php while($row = mysqli_fetch_assoc($dquery)) : ?>
                    <tr>
                        <td><?= $row['delivery_status'] ?></td>
                        <td><?= $row['starting_time'] ?></td>
                        <td><?= $row['arrival_time'] ?></td>
                        <td><?= $row['duration'] ?></td>
                        <td><?= $row['location_name'] ?></td>
                    </tr>
                <?php endwhile ?>
            <?php else : ?>
                <tr>
                    <td>No results to display</td>
                </tr>
            <?php endif ?>
            </tbody>
        </table>
    </div>
</section>

良好的阅读:

  • 什么时候应该使用预处理语句?

  • mysqli_prepare()

  • 替代语法控制结构

  • 三元运算符



文章来源: How to display corresponding mysql columns in html based on option selected from dynamic dropdown list
标签: php html mysqli