简体   繁体   中英

How to Populate Dropdown list from Database using PHP

I have a database (Rem1), which has a table (subject) with 3 columns (ID, NAME, EMAIL). This table is being regularly updated. I want to create a dropdown populated by names from this table (subject). Upon selection of subject, the value sudmitted via the form is to be the corresponding Email of the Name selected. However, my code is not working right. Please take a look.

I would greatly appreciate some help. Thank you.


<?php
    include_once "db.php";
?>

<!DOCTYPE html>


                      <select name="SELECTNAME"> 
                <?php
                    $SQLSELECT = "SELECT * FROM subject ";
                    $result_set =  mysqli_query($conn, $SQLSELECT);
                    while($row = mysqli_fetch_array($result_set))
                    {
                    ?>
     
                        <tr>
                        
  <option value="<?php echo $row['EMAIL']; ?>"><?php echo $row['NAME']; ?></option>
                         
     
                        </tr>
                    <?php
                    }
                ?>
            </table>
        </div>
     
        </div>
     
        </body>
    </html>

The db.php file is as below.

<?php
$conn=mysqli_connect("localhost","root","") or die("Could not connect");
mysqli_select_db($conn,"rem1") or die("could not connect databaseWW");
?>

You just need to enter the value of the email address.

<option value="<?php echo $row['EMAIL']; ?>"><?php echo $row['NAME']; ?></option>

At each index of the loop, you need to get the EMAIL and NAME column. As of now you are giving a static value for all the options in the dropdown.

<option value="<?php echo $row['EMAIL']; ?>"><?php echo $row['NAME']; ?></option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM