sql >> データベース >  >> RDS >> Mysql

ドロップダウンリストから対応するオプションが選択された後、それぞれのテーブルに場所が表示された回数を表示します

    私が正しく理解していれば、あなたの問題はまさに最後のものと同じ だと思います 。 WHERE句がありません。

    これが機能するかどうかを確認します:

    $con = new mysqli("localhost" ,"root" ,"" ,"user_databases");
    
    //query buildings table 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'];
        // the subquery is used to count how many times each location appears
        // for a particular building
        $count = mysqli_query($con, "
            SELECT lo.location_ID, lo.location_name, dt.num_visits
            FROM location lo
            JOIN (
                SELECT location_ID, COUNT(location_ID) AS num_visits
                FROM delivery_transaction 
                WHERE building_ID = {$selectedbldg}
                GROUP BY location_ID
            ) AS dt ON lo.location_ID = dt.location_ID
        ");
    
        // like before, better to use prepared statement
    }
    ?>
    
    <!-- ... -->
    
    <section class="row text-center placeholders">
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Location</th>
                        <th>Number of Visits</th>
                    </tr>
                </thead>
                <tbody>
                <!-- PHP alternative syntax for control structures: easier to read (imo) -->
                <!-- isset function is to ensure variable $count exist as it only gets declared in the IF condition (you would get an error otherwise) --> 
                <!-- mysqli_num_rows is to check if there are any results to loop over -->
                <?php if (isset($count) && mysqli_num_rows($count)) : ?> 
                    <?php while($row = mysqli_fetch_assoc($count)) : ?>
                    <tr>
                        <td><?= $row['location_ID'] ?></td>
                        <td><?= $row['num_visits'] ?></td>
                    </tr>
                    <?php endwhile ?>
                <?php else : ?>
                    <tr>
                        <td>No results to display</td>
                    </tr>
                <?php endif ?>
                </tbody>
            </table>
        </div>
    </section>
    

    読むべきもっと良いもの:




    1. mysqlで友達リストを取得する

    2. 複数の結合でのMySQLSUM関数

    3. to_sqlpyodbcカウントフィールドが正しくないか構文エラー

    4. SQLAlchemyの一括更新戦略