数据库
首页 > 数据库> > php-如何使用sql数据填充html下拉列表

php-如何使用sql数据填充html下拉列表

作者:互联网

我是PHP新手,希望能有所帮助.
我试图用我的SQL数据库中的数据填充HTML下拉列表.
我希望能够从下拉列表中选择一个项目,然后使用数据库中的相关记录来填充HTML表.
到目前为止,我已经设法连接到数据库并从相关表中检索所有数据.

有人可以帮我设置下拉列表中的内容吗?

谢谢

 <?php
 $username = 'root';
 $password = '';

 $conn = new PDO( 'mysql:host=localhost; dbname=Oaktown', $username, $password );
 $sql ="SELECT RoundNumber, RoundDate, HomeTeam, HomeTeamScore, AwayTeam, AwayTeamScore FROM Fixture";
 $statement = $conn->prepare( $sql );
 $statement->execute();
 $results = $statement->fetchAll( PDO::FETCH_ASSOC );

 ?>
        <h2>Competitions</h2>
<article>
    <p id="TableHeader1">Fixture Information</p>
    <P>Select Round and Game number from the dropdown list under Round Number.</P>
    <br>

    <br><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<p id="TableHeader2">Round Number &nbsp; &nbsp; 
<select style="width:250px"></select>&emsp;&emsp;<input class="button" 
type="submit" name="Get" value="Get Fixture Results"></p>

<p id="TableHeader2">Results</p>
<table class="table">
<tr><td><b>Round Number:</b></td>
<?php foreach( $results as $row ){
    echo "<td>";
    echo $row['RoundNumber'];
 }
?>
</tr>

<tr>
<td><b>Round Date:</b></td>.
<?php foreach( $results as $row ){
    echo "<td>";
    echo $row['RoundDate'];
 }
 ?>
</tr>

<tr>
<td><b>Home Team:</b></td>
<?php foreach( $results as $row ){
    echo "<td>";
    echo $row['HomeTeam'];
 }
 ?>
</tr>

<tr>
<td><b>Home Team Score:</b></td>
<?php foreach( $results as $row ){
    echo "<td>";
    echo $row['HomeTeamScore'];
   }
 ?> 
</tr>

<tr>
<td><b>Away Team:</b></td>
<?php foreach( $results as $row ){
    echo "<td>";
    echo $row['AwayTeam'];
   }
 ?> 
</tr>

<tr>
<td><b>Away Team Score:</b></td>
<?php foreach( $results as $row ){
    echo "<td>";
    echo $row['AwayTeamScore'];
   }
 ?> 
            <td colspan="2><?php echo $message; ?>"></td>
</tr>

 </table>

</form>

解决方法:

使用类似的代码块(如用于表的代码块),您可以执行以下操作:

<select>
<?php foreach( $results as $row ){
    echo "<option value='" . $row['value column'] . "'>" . $row['text column'] . "</option>";
   }
 ?> 
</select>

如果您没有或需要一对值,则只需执行以下操作:

<select>
<?php foreach( $results as $row ){
    echo "<option>" . $row['text column'] . "</option>";
   }
 ?> 
</select>

标签:html-select,html,mysql,php
来源: https://codeday.me/bug/20191025/1927648.html