简体   繁体   English

如何从数据库创建选择选项?

[英]How to create select option from database?

I'm working on a form that has 4 different select elements from 2 tables of a database. 我正在处理一个表单,该表单具有来自数据库2个表的4个不同的select元素。 I haven't done anything like this and I don't really know how to do it. 我还没有做过这样的事情,我也不知道该怎么做。

I have a table called "students" from I need "name" and "class" and a table called "books" from I need "writer" "title" ... all is one select element and all has more than 2 option values. 我有一个名为“ students”的表,我需要一个“名称”和“ class”,一个名为“ books”的表,我需要“作家”,“标题”……都是一个选择元素,并且都具有两个以上的选项值。

I've tried with only one sql query and one select but it shows only one option on the site, wether it has about 6 values in the database. 我只尝试了一个sql查询和一个select,但是它只显示站点上的一个选项,而且它在数据库中大约有6个值。

My code: 我的代码:

$sql = "SELECT class
        FROM students";
$result = mysql_query($sql);


while ($row = mysql_fetch_assoc($result)) {
    $select_class = "<option value={$row['class']}>{$row['class']}</option>";
}

<select id="class" name="class">
    <?php print $select_class; ?>
</select>

How would it be correct? 怎么会正确呢?

try this 尝试这个

$sql = "SELECT class FROM students";
$result = mysql_query($sql);

echo '<select id="class" name="class">';

while ($row = mysql_fetch_assoc($result)) {
    echo "<option value={$row['class']}>{$row['class']}</option>";
}

echo '</select>';

Changing this: 改变这个:

$select_class = "<option value={$row['class']}>{$row['class']}</option>";

to this: 对此:

$select_class .= "<option value={$row['class']}>{$row['class']}</option>";

might solve your problem. 可能会解决您的问题。

Right now you are constantly resetting the value of $select_class, instead of adding to it. 现在,您正在不断重置$ select_class的值,而不是添加到其中。 The .= assignment should help you get around this. 。=分配应该可以帮助您解决此问题。

As always, be sure to up-vote any StackOverflow answers you find useful. 与往常一样,请确保对您认为有用的所有StackOverflow答案进行投票。

You are overwriting $select_class on each while() loop. 您将在每个while()循环中覆盖$select_class You need to concatenate $select_class . 您需要连接$select_class Change to $select_class .= 更改为$select_class .=

$select_class = "";
while ($row = mysql_fetch_assoc($result)) {
    $select_class .= "<option value={$row['class']}>{$row['class']}</option>";
}

Try this 尝试这个

        <?php
                        $dbhandle = mysql_connect($hostname, $username, $password) or die("can't connect");
                        $table = "students";
                        $sql = "SELECT * FROM students";
                        $result = mysql_query($sql, $dbhandle);
                        mysql_data_seek($result, 0);
        ?>
        <form>                
        <select class="dropdown" name="dropdown">           
    <?php 
    if(mysql_num_rows($result) > 0){
        while($row = mysql_fetch_array($result)) {
            echo '<option value="' . $row['class'] . '">' . $row['class'] . '</option>';
            }
         }
    ?>
</select>
</form>

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

相关问题 如何创建从下拉列表中选择所有选项? - how to create select all option from dropdown? 如何根据第一个选项选择第二个选项(两个选项都来自数据库) - How to select the second option based on the first option (both select option are from the database) 从数据库中选择带有信息的选项 - Select option with info from database 如何取消选中选项 select 上的复选框,然后从数据库中加载相应的数据并保留复选框和选项 select state - How to uncheck checkbox on option select then load corresponding data from database and persist checkbox and option select state 如何基于单击的ID从选择选项中的数据库获取值 - how to get a value from database in select option based on clicked id 如何使用Ajax使用来自数据库的数据使用select选项填充输入? - How to use ajax to populate input with select option with data from database? 如何在select-option标记中显示mysql数据库中的值 - How to display a value from mysql database in select-option tag 如何使用选项在php中显示数据库中的图像并选择 - How to display images from a database in php using option and select 如何使用php中的select选项从数据库检索所有数据? - How to retrieve all data from database using select option in php? 如何将从数据库获取的选项添加到 select php 的末尾 - How to add option fetched from the database to the end in select php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM