简体   繁体   中英

How do I put names from table into dropdown menu?

Im trying to figure out why the dropdown doesnt show up of the list of teachers in my tables

include"teacher.php"
        <body onload="displayDate()">

        <img src="Raw Pictures/Header.jpg" style="width:100% ; height:15%">
        <img src="Raw Pictures/green.png" style="width:8%; height:11%; position:absolute; top:4% ;left: 3%">
        <br><br><br><div>
        Teacher:
        <select>

<option>echo $row</option>

       </select>
    </body>
    </html>

then the teacher.php

<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('teacher_account');

$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {}
?>

I cant figure out how to show the teachers from database into dropdown box? how do I fix it?

You are probably looking for something like that:

<?php
    mysql_connect('localhost', 'root', 'password');
    mysql_select_db('teacher_account');

    $sql = "SELECT teachername,facultyname FROM subj_eva";
    $result = mysql_query($sql);
?>
<html>
<body onload="displayDate()">
    <img src="Raw Pictures/Header.jpg" 
         style="width:100% ; height:15%">
    <img src="Raw Pictures/green.png" 
         style="width:8%; height:11%; position:absolute; top:4% ;left: 3%">
    <div>
        Teacher:
        <select>
            <?php while ($row = mysql_fetch_array($result)) { ?>
            <option value="<?= $row['teachername'] ?>">
                <?= $row['teachername'] ?> (<?= $row['facultyname'] ?>)
            </option> 
            <?php } ?>
        </select>
    </body>
    </html>

Of course the code can be separated into files, I bundled it for the sake of readability here. It probably makes sense to move those styling rules into a separate css file instead of using those ugly inline styles.

A side note: you are using the outdated and deprecated mysql extension. You should switch to either mysqli or PDO and learn about the security advantages of "prepared statements". Also you should not use the root account for normal database use. Create a less privileged account and only use the root account for administrative tasks.

<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('teacher_account');

$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);
while ($result_array = mysql_fetch_array($result, MYSQL_ASSOC) ) {
$row = $row .'<option>'. $result_array["facultyname"] .'</option>';
}
?>

and php for generate html file

<?php
include"teacher.php";
?>
        <body onload="displayDate()">

        <img src="Raw Pictures/Header.jpg" style="width:100% ; height:15%">
        <img src="Raw Pictures/green.png" style="width:8%; height:11%; position:absolute; top:4% ;left: 3%">
        <br><br><br><div>
        Teacher:
        <select>
<?php
echo $row;
?>
       </select>
    </body>
    </html>

Try this

$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);
echo "<select>";

while ($row = mysql_fetch_array($result))
{
echo "<option>$row['column']</option>";}
echo "</select>";
?>

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