dsxon40042 2015-10-28 16:28
浏览 47

在mysql的选项列表中存储行的id

I'm echoing a list of attributes of a row from a table into an option list for the user to pick from which later would be used to be inserted into another table. What I'm trying to do is, to store the 'id' of that row -not using the foreign key- separately, but the 'id' I keep getting is the 'id' of the last row on the list. Here is a snippet to my code:

        <?php
        $conn = mysqli_connect('localhost', 'root', '', 'database');

          $result = mysqli_query($conn, "SELECT * FROM myTable WHERE region = '{$thisRegion}' ");
          //$result .= " ");
          while ($row = mysqli_fetch_assoc($result))
          {
            $selected = (isset($_POST['list']) && $_POST['list'] ==  $row['id']) ? 'selected' : '';
              echo "<option value='$row[streetAddress] $row[apt] $row[city] $row[zip] $row[_state] $row[country]' $selected >$row[city] $row[zip] $row[_state] $row[country] $row[streetAddress] $row[apt]</option>";
              $thisId= $row['id'];
              $_SESSION["thisId"] = $thisId;
          }
          ?>
        </select>
  • 写回答

1条回答 默认 最新

  • drb0901500211 2015-10-28 16:41
    关注

    Your problem it's on the code

    while ($row = mysqli_fetch_assoc($result))
    {
        ...
        $thisId= $row['id'];
        $_SESSION["thisId"] = $thisId;
    }
    

    That code means that on each iteration, $_SESSION["thisId"] will be replaced with the id of the current row, making it so that at the end of the loop only the last row's ID will be there.

    The way I think to do this would be to add the id to the option, and print the row just as display.

    echo "<option value='{$row['id']}'> {$row['apt']} {$row['city']}... </option>";
    

    Then, the receiving page could read

    $selectedRowId = $_GET['optionKey']
    

    I hope that helps, you should read more about php and forms moving forward as it will make your coding easier. Here is one basic tutorial http://www.w3schools.com/php/php_forms.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?