douchen2025 2015-06-23 17:01
浏览 24

具有预选值的PHP下拉列表

I think I am nearly there with this problem, but I can't quite see why my solution isn't working. I'm trying to pre-select an item from a php drop down list that uses a MySQL table. The drop down list populates as expected, but the pre-select doesn't work. My code:

echo $Trans1E; // Display Existing value
    echo '<p>Trans1: ';
        $q = "SELECT TR1_Name FROM sb_TR1 ORDER BY TR1_Name";
        $r = @mysqli_query ($dbc, $q);
        $row = mysqli_fetch_array ($r, MYSQLI_NUM);
        echo "<select name='Trans1' value=''>Trans1</option>"; // list box select command
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
        {//Array of records stored in $row
            $selected='';
            if($row[TR1_Name]==$Trans1E) //determine if the row value is the same as the Existing
            {//if it it then mark as selected
                echo "<option value=$row[TR1_Name] selected='selected'>$row[TR1_Name]</option>";
            }
            else
            {// if it is not then just inlcude it in the drop down list
                echo "<option value=$row[TR1_Name] >$row[TR1_Name]</option>";
            }
        }
        echo "</select>";// Closing of list box
    echo '
    </p>';

I've seen similar things on this forum for drop down lists which I've incorporated in my code or at least experimented with but without success, I suspect an error in: if($row[TR1_Name]==$Trans1E), but have run out of thinsg to try.

  • 写回答

1条回答 默认 最新

  • duanchu2607 2015-06-26 06:28
    关注

    It seems I was closer to the correct code than I thought, I resolved the problem by adding the line:

    $Trans1E=mysqli_real_escape_string($dbc, trim(htmlentities($Trans1E)));
    

    before the code. Despite displaying the content of Trans1E, it seems there was an unwanted element in it, which I hadn't spotted so I thought the code further down was at fault. Thanks for you comments. Hope this helps others.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP