繁体   English   中英

从mysql / php参考的下拉列表中选择的选项

[英]Selected option in dropdown from mysql/php reference

尝试根据存储在mysql数据库中的引用在下拉列表中选择一个选项。

这是在while循环内生成表期间发生的。

我已经尝试了以下重点

<option selected='<? if ( $row2xsecid == $row1x[item_sub_sec_id]) { echo "selected"; } ?>' value="<? echo $row1x[item_sub_sec_id]; ?>"><? echo $row1x[item_sub_sec_title]; ?></option>

我似乎无法根据存储在mysql中的引用来选择选项。

片段

  <div class="input-group">
                    <span class="input-group-addon">Select Group</span>
                    <select id='add_item_groupid' class="form-control">
                    <?
                    $sql1x = 'SELECT * FROM items_sub_section_list ORDER BY item_sub_sec_id ASC';
                    $result1x = mysql_query($sql1x);

                    while ($row1x = mysql_fetch_array($result1x)) {
                    ?>
                            <?
                            $sql2x = 'SELECT * FROM items_groups WHERE item_id=$itemid';
                            $result2x = mysql_query($sql2x);
                            while ($row2x = mysql_fetch_array($result2x)) { $row2xsecid = $row2x[item_sub_sec_id]; }
                            ?>
                        <option selected='<? if ( $row2xsecid == $row1x[item_sub_sec_id]) { echo "selected"; } ?>' value="<? echo $row1x[item_sub_sec_id]; ?>"><? echo $row1x[item_sub_sec_title]; ?></option>

                    <?
                    } 
                    ?> 
                    </select>
    </div>

完整脚本的pastebin - http://pastebin.com/NZrETate

我看到您正在获取数组而不是关联数组。 (第27行:mysql_fetch_array而不是:mysql_fetch_assoc)

为此,您必须记住字段的索引以0开头并且是数字,这意味着它将返回未定义的索引:

line40: <td><? echo $row['item_id']; ?></td>

因为您获取了一个数组,这还将返回未定义的索引:$ itemid = $ row ['item_id']; 由于$ itemid是未定义的,因此第49行的查询也会失败:

 $secid = mysql_query(" SELECT * FROM item_groups WHERE item_id='$itemid' ");

我也看不到哪里声明了item_sub_sec_id? (在PHP中,如果这是一个变量,则必须在前面加$:$ item_sub_sec_id)

您能够看到PHP抛出的错误吗? 如果没有,我建议显示所有错误,如下所示:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

一旦您解决了这些问题,我们就可以解决问题,如果我们知道数据库结构是什么样的,那么对我来说,为您提供解决方案也将更加容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM