php 从选定的数据库中选择选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15391646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:08:57  来源:igfitidea点击:

select option value from database on selected

phpmysqlformscombobox

提问by Jsmith

I have a small problem. I have a edit.php page. This page list the products information that can be edited.

我有一个小问题。我有一个 edit.php 页面。此页面列出了可以编辑的产品信息。

I run a query

我运行一个查询

while($rows=mysql_fetch_assoc($query)){


echo"<form method=\"POST\" action=\"edit.php\">";
echo "<input type ='hidden' name='ID' value = '{$rows['ID']}'>"; 
echo "Product:&nbsp <input type='text' name='product' value = '{$rows['ProductName']}'>"; 

Before the while loop I store the details in variables as such:

在 while 循环之前,我将详细信息存储在变量中,如下所示:

$hiddenid = $_POST['ID'];   
$productName = $_POST['product'];

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.

这有效。当我加载 php 表单时,它会显示从数据库中检索到的产品名称(在文本字段中)。但是问题是我想将产品名称存储为用户已经选择的下拉列表框,然后选择它。

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.

所以,基本上我希望做的不是在文本框中显示数据库检索的选项,而是希望用户选择的选项显示在下拉列表框中。

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).

我希望这是有道理的?为什么我的选项值根本没有显示,其次它们也没有显示 SELECTED 选项(从数据库中检索)。

Any help please?

请问有什么帮助吗?

回答by UnholyRanger

Focusing on this one area, you should be getting a PHP error unless it is a copy error. Notice the following changes:

专注于这一领域,除非是复制错误,否则您应该会收到 PHP 错误。请注意以下更改:

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

You had plain HTML code in the PHP segment in the beginning.

一开始,您在 PHP 段中有纯 HTML 代码。

Also, I believe you want

另外,我相信你想要

if ($value == $productName){

回答by Absolute?ER?

This probably isn't such a good place for this, but since I see tons of code daily that is horrible I figured I would chime in.

这可能不是一个很好的地方,但由于我每天看到大量可怕的代码,我想我会加入。

First of all, check your inputs. You should never trust any variables from a user $_GET, $_POST, etc. The looseness of the original code is just looking for an SQL injection attack.

首先,检查您的输入。你永远不应该相信来自用户 $_GET、$_POST 等的任何变量。原始代码的松散只是在寻找 SQL 注入攻击。

<?php

This thing looks like it's expecting some sort of passed ID which would then pull the productName from the database... it's really unclear. So we're assuming variables are already defined. Need to use code like this:

这东西看起来像是在期待某种传递的 ID,然后它会从数据库中提取 productName ......这真的不清楚。所以我们假设变量已经定义。需要使用这样的代码:

if(!empty($_GET['id'])&&is_numeric($_GET['id'])){
   $id=$_GET['id'];
} else {
   $id='';
}

//Declare your array
$arrayproducts = array();


while($rows=mysql_fetch_array($query)){

You need to call something out here for the returned array... without seeing the SQL it's hard to say what you're expecting, but your callouts should look like this...

您需要在此处为​​返回的数组调用一些内容......没有看到 SQL 很难说出你的期望,但你的标注应该看起来像这样......

//Whatever the name from SQL is for the column
$temp_ID = $rows['id'];

//Whatever the name for the product column is
$temp_Prod = $rows['prod'];

//Load the array
$arrayproducts[$temp_ID]=$temp_Prod;
}

It's best to load a var for everything and then post it in one shot rather than in and out of PHP. The code will be much faster and you'll be able to keep track of the code.

最好为所有内容加载一个 var,然后一次性将其发布,而不是在 PHP 中进出。代码会快得多,您将能够跟踪代码。

$page = "<form method=\"POST\" action=\"edit.php\">";

Check to see if that ID worked. Regex would be best, but one thing at a time here.

检查该 ID 是否有效。正则表达式会是最好的,但在这里一次做一件事。

if(!empty($id)){
    $page .= "<input type =\"hidden\" name=\"ID\" value = \"$id\">";
} 
$page .= "<select name =\"pnames\">\n";

foreach ($arrayproducts as $key => $value) {

   $page .= "<option value = \"$key\"";

It's best to use the auto-increment fields and built-in IDs for mysql so there isn't a chance of having a duplicate ID taking over an existing record.

最好为 mysql 使用自动增量字段和内置 ID,这样就不会有重复的 ID 接管现有记录的机会。

      if ($id == $key){
        $page .= ' selected="selected"';
      }
   $page .= ">$value</option>\n";

//close the foreach
}

$page .= "</select>\n"; 

print $page;
?>