ユーザーが動的ドロップダウンボックスからオプションを選択した後、対応するmysqlテーブルの列を表示するためのヘルプをお願いしたいと思います。どこが間違っていたのか本当にわかりません:(助けてください:(
私は3つのmysqlテーブルを持っています:buildings、delivery_transaction、location、すべて互いに接続されています。メインテーブルは配送トランザクションです。
[delivery_transactionテーブル。building_IDとlocation_IDは、残りの2つのテーブルのFKです] [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
私が正しく理解していれば、すべての建物を表示するための1つのドロップダウンがあります。
提出時に、選択した建物に基づいた配達の表を提示します。
これは2つのクエリです(1つしかありません)。
これが私がそれをする方法です:
// 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>
読むのが良い:
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加