【问题标题】:Dynamically displaying option values using PHP使用 PHP 动态显示选项值
【发布时间】:2015-01-24 06:15:52
【问题描述】:

我是 PHP 新手,创建了一个非常基本的 HTML 表单。正如你在我的表格中看到的,选项值都是手工完成的(还有更多,我只是简化了这个例子)。我想要的是仅使用 PHP 动态生成这些,这样我就必须每年添加一次等等。

我已经进行了一些搜索,但我似乎无法准确找到我想要的东西,所以我想在这里问一下。根据我收集的信息,我需要创建一个查询并以某种方式回显选项值,尽管我不确定如何执行此操作。

SELECT gameYear from games

我猜以上将是正确的查询,因为所有表单需要的是表中的 bookYear?

<form id = "gameYear" method="get" action="result.php">
<label> 
    Game Year
    <select name="gameYear">
       <option value="2000">2000</option>
       <option value="2001">2001</option>
       <option value="2002">2002</option>
    </select>
</label>
   <input type = "submit" name="search" value = "Search">
</form>

谢谢,感谢任何帮助/指导。

【问题讨论】:

    标签: php html mysql forms dynamic


    【解决方案1】:
    <form id = "gameYear" method="get" action="result.php">
    <label> 
    Game Year
    <select name="gameYear">
    <option value=''>--Select Year--</option>
    <?php
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    $SqlResult = mysqli_query($link, "SELECT gameYear from games");
    while($Row = mysqli_fetch_array($SqlResult))
    {
      ?>
      <option value="<?php echo $Row['gameYear'] ?>"><?php echo $Row['gameYear'] ?></option>
    }
    ?>
    </select>
    </label>
    <input type = "submit" name="search" value = "Search">
    </form>
    

    【讨论】:

      【解决方案2】:
      <?php $sql = "SELECT gameYear from games order by gameYear ASC";
          $result = mysql_query($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); ?>
      
      
      <form id="gameYear" method="get" action="result.php">
      <label>Game Year
      <select name="gameYear">
      <?php while($row = mysql_fetch_array($result)){ ?>                        
      <option value="<?php echo $row['gameYear'] ?>"><?php echo $row['gameYear']?></option>
      <?php } ?>
      </select>
      </label>
      <input type = "submit" name="search" value = "Search">
      </form>
      

      【讨论】:

      • 您可能还想添加ORDER BY gameYear ASC。你的&lt;option value="&lt;?php echo $row['gameYear'] ?&gt;"&gt;&lt;?php echo $row['fullname']?&gt;&lt;/option&gt; 也应该是
      猜你喜欢
      • 2014-11-22
      • 2022-06-10
      • 2017-05-11
      • 2014-06-02
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多