简体   繁体   English

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

[英]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 然后是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. 将这些样式规则移动到单独的css文件中,而不是使用那些难看的内联样式可能是有意义的。

A side note: you are using the outdated and deprecated mysql extension. 附带说明:您正在使用过时和不建议使用的mysql扩展。 You should switch to either mysqli or PDO and learn about the security advantages of "prepared statements". 您应该切换到mysqliPDO并了解“预备语句”的安全性优势。 Also you should not use the root account for normal database use. 另外,您不应将root帐户用于正常的数据库使用。 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生成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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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