如何将表格中的名称放入下拉菜单?

时间:2015-02-21 11:40:18

标签: php html mysql

我试图找出为什么下拉列表没有出现在我的表格中的教师名单

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>

然后是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)) {}
?>

我无法弄清楚如何将数据库中的老师展示到下拉框中?我该如何解决?

3 个答案:

答案 0 :(得分:1)

你可能正在寻找类似的东西:

<?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>

当然代码可以分成文件,为了便于阅读,我把它捆绑在一起。将这些样式规则移动到单独的css文件中而不是使用那些丑陋的内联样式可能是有意义的。

附注:您使用的是过期且已弃用的mysql扩展程序。您应切换到mysqliPDO,并了解“预备语句”的安全优势。此外,您不应使用root帐户进行正常的数据库使用。创建权限较低的帐户,仅将root帐户用于管理任务。

答案 1 :(得分:1)

<?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>';
}
?>

和php生成html文件

<?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>

答案 2 :(得分:0)

试试这个

$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>";
?>