为什么选择选项总是在 MySQL 表中发布最后一个值


why does select option always post last value in mysql table

我有一个表单选择框,它正确填充了 mysql 表中的值,但是当它被发布时,发布的值是表格"manutags"中的最后一个项目,而不是框中显示的所选项目。有人可以告诉我我的逻辑有什么问题吗?

<p class="formlabel cf text nobox">Tag Style  <!--popuplate with tag styles-->
<?$i=1;
    while($i<11){?>         
        <select class="man_sty <?$m='man'.$i++;echo $m?>" name="style_id">
        <?
            $sql = "SELECT DISTINCT man,style_id FROM manutags WHERE man_id = '$m'";
            $result = mysql_query($sql) or die(mysql_error());
        while($row = mysql_fetch_array($result)){
        ?>
         <option value="<?=$row[1];?>"><?echo $row[1]?></option><!-- tag dropdown values-->
        <?}?>
        </select>
    <?}?>

.HTML

<p class="formlabel cf text nobox">
Tag Style
<select class="man_sty man1" name="style_id_1" style="display: none;">
<option selected="" value="Kliktag">Kliktag</option>
<option value="RD2000">RD2000</option>
<option value="Button-R">Button-R</option>
<option value="SnappTagg">SnappTagg</option>
<option value="AutoEID">AutoEID</option>
</select>
<select class="man_sty man2" name="style_id_2" style="display: none;">
<option selected="" value="rototag">rototag</option>
</select>
<select class="man_sty man3" name="style_id_3" style="display: inline;">
<option selected="" value="Qwik">Qwik</option>
<option value="Zee">Zee</option>
</select>
</p>

您的所有菜单都有相同的name="style_id"。因此,当您提交表单时,它们都设置$_POST['style_id']。由于只有一个变量具有该名称,因此您只能获得最后一个变量。您需要为它们指定不同的名称,以便从每个名称中进行选择。

尝试:

<select class="man_sty <?$m='man'.$i++;echo $m?>" name="style_id[]">

然后$_POST['style_id']将是一个包含每个选择值的数组。

使您的下拉名称动态化。 对于 10 下拉 10 个不同的名称。 然后选择动态下拉的值。 思考会给你所需的结果。

您正在输出 10 个选择框,但您的问题显示"表单选择框"。 如果您只需要一个,请将选择标签移到循环外:

<p class="formlabel cf text nobox">Tag Style  <!--popuplate with tag styles-->
<select class="man_sty <?$m='man'.$i++;echo $m?>" name="style_id">
<?php
$i=1;
while($i<11){
    $sql = "SELECT DISTINCT man,style_id FROM manutags WHERE man_id = '$m'";
    $result = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    ?>
        <option value="<?=$row[1];?>"><?echo $row[1]?></option><!-- tag dropdown values-->
    <?php
    }
}
?>
</select>

如果您确实想要 10 个选择框,那么您可能希望对参数使用数组语法:

<p class="formlabel cf text nobox">Tag Style  <!--popuplate with tag styles-->
<?php
$i=1;
while($i<11){
    ?>
<select class="man_sty <?$m='man'.$i++;echo $m?>" name="style_id[]">
    <?php
    $sql = "SELECT DISTINCT man,style_id FROM manutags WHERE man_id = '$m'";
    $result = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    ?>
        <option value="<?=$row[1];?>"><?echo $row[1]?></option><!-- tag dropdown values-->
    <?php
    }
    ?>
    </select>
    <?php
}
?>

然后在接收端,您将收到一个 $_POST['style_id'] 数组(假设您在表单中使用 method="post")。

看看吧。似乎您忘记更改每个选择的name

<?php
for ($i = 1; $i <= 10; $i++) {
    $manId = "man" . $i;
    echo "<select class='"man_sty {$manId}'" name='"style_id_{$i}'">";
    $sql = "SELECT DISTINCT man, style_id FROM manutags WHERE man_id = '{$manId}'";
    $result = mysql_query($sql) or die(mysql_error());
    $selected = 'selected';
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='"{$row[1]}'" {$selected}>{$row[1]}</option>";
        $selected = '';
    }
    echo "</select>";
}
?>