dtp87205 2016-01-04 19:05
浏览 72

填充的mysql中的未定义索引选择设置时选择

I have a select dropdown that is being populated and displayed correctly from a mysql query:

  <select name="title">
  <option></option>
  <?php
    while($row = mysql_fetch_assoc($title_data)) {
      echo '<option value=' . $row["title"];
      echo '>' . $row["title"];
      echo '</option>';
    } 
  ?>
  </select>

When I add code to check which row was selected, I get an undefined index error for each row in the query results:

  <select name="title">
  <option></option>
  <?php
    while($row = mysql_fetch_assoc($title_data)) {
      echo '<option value=' . $row["title"];
      if($_POST["title"] == $row["title"]) {
        echo ' selected';
      }
      echo '>' . $row["title"];
      echo '</option>';
    } 
  ?>
  </select>

I'm realtively new to this and may be missing something obvious. I had checked the posts and did not see this particular example where I am using a while loop from a query and trying to echo the selected attribute. The actual error text is inserted into the dropdown box:

Notice: Undefined index: title in C:\xampp\htdocs\indexdb.php on line 215 >Analyst I" and appears for each row in the dropdown box.

I apologize if this has been covered as I stated, this is my first php code and is a protype only. This is the only hurdle I have not been able to overcome.

  • 写回答

1条回答 默认 最新

  • douyong1974 2016-01-05 15:36
    关注

    You need to encapsulate the value attribute's data.

    echo '<option value="' . $row["title"] . '" ';
    

    Then to resolve the undefined index first check that the field is populated:

    if(!empty($_POST["title"]) && $_POST["title"] == $row["title"]) {
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行