从数据库中选择选项值 [英] select option value from database on selected
问题描述
我有一个小问题。我有一个edit.php页面。
I have a small problem. I have a edit.php page. This page list the products information that can be edited.
我运行一个查询
while($rows=mysql_fetch_assoc($query)){
echo"<form method=\"POST\" action=\"edit.php\">";
echo "<input type ='hidden' name='ID' value = '{$rows['ID']}'>";
echo "Product:  <input type='text' name='product' value = '{$rows['ProductName']}'>";
在while循环之前,我将细节存储在变量中:
Before the while loop I store the details in variables as such:
$hiddenid = $_POST['ID'];
$productName = $_POST['product'];
当我加载php形式它显示从数据库检索的产品名称(在文本字段)。但是,问题是我想将产品名称存储为已经由用户选择的下拉列表框,然后选择它。
and this works. when I load the php form it shows the product name (in a text field) retrieved from the DB. However the problem is I want to store the product name as a drop down list box that has already been selected by the user and then select that.
因此,基本上是什么希望做的是不是在文本框中显示DB检索的选项我希望用户选择的选项显示在下拉列表框中。
So, basically what I wish to do is instead of displaying the DB retrieved options in a text box I wish the option the user has selected to be displayed in a dropdown list box.
我希望这会使sence吗?为什么我的选项值不显示,其次它们没有显示SELECTED选项(从数据库检索)。
I hope this makes sence? Why are my option values not showing at all and secondly they are not showing the SELECTED option either (retrieved from the DB).
任何帮助吗?
推荐答案
关注这一个领域,你应该得到一个PHP错误,除非它是一个复制错误。请注意以下更改:
Focusing on this one area, you should be getting a PHP error unless it is a copy error. Notice the following changes:
while($rows=mysql_fetch_assoc($query)){
?>
<form method=\"POST\" action=\"edit.php\">
<input type ='hidden' name='ID' value = '<?php echo $rows['ID'];?>'>
<select name ="pnames">
<?php foreach ($arrayproducts as $key => $value) {
?>
<option value = "<?php echo $key; ?>"
<?php
if ($key == $productName){
echo 'selected="selected"';
}
?> >
<?php echo $value; ?>
</option>
<?php } //end foreach ?>
</select>
<?php }//end while ?>
您在开头的PHP段中有纯HTML代码。
You had plain HTML code in the PHP segment in the beginning.
此外,我相信你想要
if ($value == $productName){
这篇关于从数据库中选择选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!