PHP-下拉菜单(选择)无法正常运行至while循环 [英] PHP - Dropdown menu(select) not working properly to while loop

查看:141
本文介绍了PHP-下拉菜单(选择)无法正常运行至while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的dropdown-textbox-select(我很困惑,它实际上是什么)可以查询要显示的适当表中的所有数据.但是它将显示所有数据,并带有单独的dropdown-textbox-select.我只希望数据在内部.到目前为止,这是我正在做的事情.

I want my dropdown-textbox-select(I'm confused what it really is) to query all the data in the appropriate table I want to display. But it is displaying all the data with separated dropdown-textbox-select. I just want the data to be inside. Here's what I'm doing so far.

<?php   


    $sel_admin = "SELECT * FROM author";
    $rs_admin = mysql_query($sel_admin);
    while($row = mysql_fetch_array($rs_admin))
                {
                echo '<select class="form-control">';
                echo"  <option value='volvo'>" . $row['author_firstname'] . $row['author_lastname'] ."</option>";
                echo'</select>';
                }
                ?>

PS.如果用户单击提交,我希望保存author_id而不是名称.怎么可能呢?

PS. If the user click submit, I want the author_id to be save, instead of the name. How can this be also?

推荐答案

您的select标记应位于while循环之外:您只需要一个选择字段,其中包含所有已提取行的选项.

Your select tag should be outside of the while loop : you want only one select field, containing options with all the fetched rows.

echo '<select class="form-control">';
while($row = mysql_fetch_array($rs_admin))
{
    echo"  <option value='volvo'>" . $row['author_firstname'] . $row['author_lastname'] ."</option>";
}
echo'</select>';

如果要从此表单获取author_id,只需将此值放在选项的value属性中即可.

If you want to get the author_id from this form, just put this value in the option's value attribute.

echo"  <option value='". $row['author_id'] ."'>" . $row['author_firstname'] . $row['author_lastname'] ."</option>";

别忘了给select标记加上name.

这篇关于PHP-下拉菜单(选择)无法正常运行至while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆