1

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
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • Off the bat, your query does not include `building_ID` column and you never defined nor set `$selectedbldg`. – Mikey Jan 23 '18 at 17:36

1 Answers1

0

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:

Mikey
  • 6,728
  • 4
  • 22
  • 45
  • Hi Mikey! Thanks so much for the big help! Realized I've been missing a lot of lines all these time. Sorry for the late response. Yeah, you understood it right. I've tried adding the lines of code you suggested, however I got this error after clicking the 'submit' button: "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result" – rookie_programmer Jan 24 '18 at 14:29
  • 1
    @rookie_programmer Oops. Change `$dquery = mysqli_prepare(...)` to `$dquery = mysqli_query(...)`. (Though you should be using prepare.) – Mikey Jan 24 '18 at 14:30
  • Yey! Thanks! However, I got another error. Each column displays "Notice: Undefined index" for delivery_status, starting_time, arrival_time, duration and location_name. Do I have to query them again inside the
    class? Sorry so many questions :(
    – rookie_programmer Jan 24 '18 at 14:37
  • @rookie_programmer I don't really use mysqli (as [PDO](http://php.net/manual/en/book.pdo.php) is easier to use). I believe you need to use [mysqli_fetch_assoc](http://php.net/manual/en/mysqli-result.fetch-assoc.php) instead of [mysqli_fetch_row](http://php.net/manual/en/mysqli-result.fetch-row.php) throughout your code. – Mikey Jan 24 '18 at 14:41
  • 1
    Hi Mikey! Sorry, I was able to figure it out. I just had to change the indexes from say $row['delivery_status'] to $row[0], the corresponding index it has under the $dquery. Thank you so so much Mike! great help :D The links you included for reading are also great. – rookie_programmer Jan 24 '18 at 14:44
  • 1
    @rookie_programmer In terms of readability, I would use mysqli_fetch_assoc instead, so that you don't have to remember the index of each column. And no problem. – Mikey Jan 24 '18 at 14:46
  • 1
    This is well noted Mikey! I have already changed it into mysqli_fetch_assoc. :) – rookie_programmer Jan 24 '18 at 14:49
  • Hi mikey! I'd like to ask another question related to this one. Is it possible to create a separate html table where I would display the number of times the location names appeared in my delivery_transaction table? – rookie_programmer Jan 25 '18 at 14:29
  • 1
    @rookie_programmer It's usually better to post a new question (a notice will most likely appear shortly to avoid long discussions). But to your question, it is possible. You would need to produce a new SQL query that will more than likely involve [GROUP BY](https://www.w3schools.com/sql/sql_groupby.asp) e.g. `SELECT location_ID, COUNT(*) AS num_deliveries FROM delivery_transaction GROUP BY location_ID`. (I'm assuming each location_name has a unique location_ID.) – Mikey Jan 25 '18 at 15:23
  • Hi Mikey! I posted this as a new question so I can better explain what my question really is in this thread: https://stackoverflow.com/questions/48446624/display-the-number-of-times-a-location-appeared-in-the-respective-table-after-co. thanks! – rookie_programmer Jan 25 '18 at 15:40