代码之家  ›  专栏  ›  技术社区  ›  Zaki Hussain

从数据库表列获取变量到基于输入字段的选项从选项字段

  •  -2
  • Zaki Hussain  · 技术社区  · 6 年前

    问题是我无法将第三列中的数据显示到输入字段中,该字段也在表单中,基于所选选项。

    任何帮助都将不胜感激。相关代码如下。

         <div class="col-md-4 col-12 bottommargin-sm">
          <label for="">Choose currency</label>
          <select class="form-control" id="exampleFormControlSelect1">
    
            <option value="">Choose currency...</option>
    
            <?php
    
            // Including DB connection file
            include_once "config/config.php";
    
            // Retrieving all columns from currency table
            $result = mysqli_query($con, "SELECT * FROM currency_test");
    
            // Loop printing options with country and currrency for all rows in currency table
            while($row = mysqli_fetch_array($result)) {
    
              print "<option>";
    
              echo $row['country'] . " - " . $row['currency'];
    
              print "</option>";
    
            }
    
            // Closing DB connection
            mysqli_close($con);
    
            ?>
    
          </select>
        </div>
    
        <div class="col-md-2 col-12 bottommargin-sm">
          <label for="">Currency rate</label>
          <input type="text" class="form-control" value="<?php echo $row['buy_rate'] ?>" readonly>
        </div>
    

    附加编辑

    我现在使用以下代码,仍然没有任何运气:

         <div class="col-md-4 col-12 bottommargin-sm">
          <label for="">Choose currency/label>
          <select class="form-control" id="exampleFormControlSelect1">
    
            <option value="default">Choose currency...</option>
    
            <?php
    
            // Including DB connection file
            include_once "config/config.php";
    
            // Retrieving all columns from currency table
            $result = mysqli_query($con, "SELECT * FROM currency_test");
    
            // Loop printing country, currrency, buy_rate and sell_rate for all rows in currency table
            while($row = mysqli_fetch_array($result)) {
    
              echo "<option value=".$row['currency']."> ".$row['country']. " - " .$row['currency']." </option>";
    
            }
    
            ?>
    
          </select>
        </div>
    
        <div class="col-md-2 col-12 bottommargin-sm">
          <label for="">Currency rate</label>
          <input type="text" class="form-control" value="<?php echo $row['buy_rate'] ?>" readonly>
        </div>
    
        <?php
    
        // Closing DB connection
        mysqli_close($con);
    
        ?>
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Asisito    6 年前

    如前所述,您在上面的while循环中有一个结果集,但是您希望输入字段只包含一个值:所选货币的买入汇率。如果我猜对了,这就是你想要的,这就不会像这样工作了。

    如果您想测试它,并且只在while循环中检索一个值,那么您的输入字段也会被正确的购买率填充。

    可以使用JavaScript也可以不使用JavaScript,但需要另一条语句来检索所需的购买率。

    进一步的安全建议:不应使用不带DIR的include指令。也不应该使用“选择*…”。最好是命名所有要检索的字段。

        2
  •  -1
  •   Dieter Kräutl    6 年前

    现在,当您提交表单时,您可以查询数据库并显示正确的记录。

    如果不想使用submit按钮,并且文本字段中的值按选择进行更改,则需要javascript。

        3
  •  -2
  •   fransyozef    6 年前

    我看到的是:

    <select class="form-control" id="exampleFormControlSelect1">
    

    没有名称属性。所以在发布表单之后,服务器不知道变量的名称。

    2) 没有价值

    print "<option>";
    

    这是选择标记的正确方式: https://www.w3schools.com/tags/tag_select.asp