HTML / PHP / SQL - 在选定的下拉选项中显示与sql条目相关的信息

问题描述:

So basically, if you didn't understand the question, I have a dropdown menu with car brands (TABLE BRANDS), and when you select a certain option like "Volkswagen" it should display information about car models that are related to the brand "Volkswagen". The car models are in a table called CARS and for example when you select "Volkswagen", below it should display "Polo", "Golf" etc (every entry that is connected with the foreign key brand_id which is located in CARS). How can I make this happen ? Here's where I've got so far:

<?php
                    $sql = "SELECT brands.name, cars.id, cars.model, cars.mileage, cars.color FROM brands, cars WHERE brands.id = cars.brand_id ORDER BY cars.model DESC, brands.name;";
                    $result = $mysqli->query($sql);

                    $sql1 = "SELECT id, name FROM brands";
                    $result1 = $mysqli->query($sql1);

                ?>


<select>
           <?php 
            while ($row = mysqli_fetch_array($result1))
            {
              echo '<option value="'. $row['id'] .'">'. $row['name'] .'</option>';
            }
            ?>
            </select>

The code is almost right. You should use the dropdown in a form and submit it to another file or php page. On another page, you can use the php code and process the result over there. Get the input from dropdown and submit it to php page.