从填充的下拉列表中获取价值
问题描述:
我已经从数据库中填充了一个下拉列表,但我不认为该值被设置为用户从下拉列表中选择的名称,因为在选择表单时,没有向数据库添加任何值.
I have populated a drop down list from the database but i dont think the value is being set to the name the user chooses from the drop down as when the form is selected, no value is added to the database.
目前,它显示了该表中的所有名称.正如我所说,我希望该值是用户选择的名称,以便在提交表单时将其添加到数据库中并可以在另一个页面上获取.
At the moment, it shows all the names from that table. As i said, i want the value to be the name the user chooses so that when the form is submitted it is added to the database and can be fetched on another page.
<select name="category">
<?php
$sql = mysql_query("SELECT category_Name FROM category");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$category_Name.'">' . $row['category_Name'] . "</option>";
}
?>
答
echo '<option value="'.$category_Name.'">' . $row['category_Name'] . "</option>";
// ^ this var is never set
应该
echo '<option value="'.$row['category_Name'].'">' . $row['category_Name'] . "</option>";
// ^ change this
或者你可以这样拥有
echo '<option>' . $row['category_Name'] . "</option>";
如果未设置值,则只取显示名称的值.
If value is not set, it just takes the value of the display name.