如何根据从动态下拉列表中选择的选项在html中显示相应的mysql列 [英] How to display corresponding mysql columns in html based on option selected from dynamic dropdown list

查看:89
本文介绍了如何根据从动态下拉列表中选择的选项在html中显示相应的mysql列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户从动态下拉框中选择一个选项后,我想要求帮助显示相应的mysql表列。我真的不知道我在哪里出了问题:(请帮忙:($ / b>

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

[delivery_transaction表,其中building_ID和location_ID是剩余2个表中的FK] [1]



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



 < form name =bldg_formmethod =postaction => ; 

<?php

  //建立与db的连接


$ con = new mysqli(localhost, !,,user_databases);

if(!$ con)
{
echo无法连接!;
}

//从delivery_transaction,建筑物和位置表中选择列
$ 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);
?>
<! - 创建下拉框 - >
< select name ='bldg'>
< option value =>选择建筑< / 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']。'>'。$行[ 'building_name'] '< /选项>'。
}
}
?>
< / select>
< input type =submitname =view/>
< / form>

< section class =row text-center placeholders>
< div class =table-responsive>
< p>
< table class =table table-striped>
< thead>
< tr>
< th>传送状态< / th>
< th>开始时间< / th>
< th>到达时间< th>
< th>持续时间< / th>
< th>位置< / 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 ['arrival_time']。< / td>。
< td>。
< td>。$ row ['location_name']。< / td>。< / td> / TR>中;
}
}
else
{
echo没有结果显示;
}
?>
< / tr>
< / tbody>
< / table>
< / p>
< / div>
< / section>
< / main>

我想要做的是如果用户点击一个选项,它会显示
对应表,就像我查询的一样。但是,没有显示:(

[此链接显示用户选择一个选项] [2]
[1]:https://i.stack.imgur.com/H78Gp.png
[2]:https://i.stack.imgur.com/pA6gI.png


解决方案

如果我理解正确,您可以使用一个下拉菜单来显示所有建筑物。



提交后,这是2个查询(你只有一个)。



这是我如何做到这一点:

  //开发开发PHP错误报告绝不是一个坏主意
error_reporting(E_ALL) ;
ini_set('display_errors',1);

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

//总是查询建筑物bc我们需要它作为下拉菜单
$ bquery = mysqli_query($ con,SELECT building_ID,building_name FROM buildings);

$ selectedbldg = null;

//如果表单提交了
if(!emp ty($ _ POST ['bldg'])){
// store selected building_ID
$ selectedbldg = $ _POST ['bldg'];
//基于建筑物查询交付;
//注意附加条件(我假设building_ID是一个整数)
$ 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}
);

//尽管它不是您问题范围的一部分,但您应该使用
之前的准备语句
?>


< form name =bldg_formmethod =postaction =>
< select name =bldg>
< option value =>选择建筑< / 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 =submitname =view/>
< / form>


< section class =row text-center placeholders>
< div class =table-responsive>
< table class =table table-striped>
< thead>
< tr>
< th>传送状态< / th>
< th>开始时间< / th>
< th>到达时间< th>
< th>持续时间< / th>
< th>位置< / 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>无结果显示< / td>
< / tr>
<?php endif?>
< / tbody>
< / table>
< / div>
< / section>

好读:


I would like to ask for help in displaying the corresponding mysql table columns after the user selects an option from the dynamic dropdown box. I really don't know where I went wrong :( please help :(

I have 3 mysql tables: buildings, delivery_transaction and location, all connected to each other. The main table is the delivery transaction:

[delivery_transaction table, where building_ID and location_ID are the FKs from the remaining 2 tables][1]

Wherein if user will click on whatever building name is present in dropdown list, it will display only the columns I queried from the main table as follows.

Here's my code so far:

<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

解决方案

If I understood correctly, you have one dropdown to show all the buildings.

Upon submission, show a table of deliveries based on the selected building.

That is 2 queries (you only have one).

Here's how I would do it:

// 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>

Good to read:

这篇关于如何根据从动态下拉列表中选择的选项在html中显示相应的mysql列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆