从 PHP 中的 mySQL 表填充下拉框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5189662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:35:09  来源:igfitidea点击:

Populate a Drop down box from a mySQL table in PHP

phpmysql

提问by Donnie

I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)

我试图在 PHP 中从 mySQL 查询的结果填充下拉框。我在网上查找了一些示例,并在我的网页上尝试了它们,但出于某种原因,它们根本没有填充我的下拉框。我试图调试代码,但在我查看的网站上并没有真正解释它,我无法弄清楚每一行代码是什么。任何帮助都会很棒:)

Here's my Query: Select PcID from PC;

这是我的查询: Select PcID from PC;

回答by Dan Grossman

You will need to make sure that if you're using a test environment like WAMP set your username as root. Here is an example which connects to a MySQL database, issues your query, and outputs <option>tags for a <select>box from each row in the table.

您需要确保如果您使用的是 WAMP 之类的测试环境,请将您的用户名设置为 root。这是一个连接到 MySQL 数据库、发出查询并从表中的每一行输出框的<option>标签的示例<select>

<?php

mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);

echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";

?>

回答by sush

Below is the code for drop down using MySqland PHP:

下面是使用MySqland下拉的代码PHP

<?
$sql="Select PcID from PC"
$q=mysql_query($sql)
echo "<select name=\"pcid\">"; 
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q)) 
{        
echo "<option value='".$row['PcID']."'>".$row['PcID']."</option>"; 
}
echo "</select>";
?>

回答by motorbaby

Since mysql_connecthas been deprecated, connect and query instead with mysqli:

由于mysql_connect已被弃用,请改为使用 mysqli 连接和查询:

$mysqli = new mysqli("hostname","username","password","database_name");
$sqlSelect="SELECT your_fieldname FROM your_table";
$result = $mysqli -> query ($sqlSelect);

And then, if you have more than one option list with the same values on the same page, put the values in an array:

然后,如果在同一页面上有多个具有相同值的选项列表,请将这些值放入一个数组中:

while ($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

And then you can loop the array multiple times on the same page:

然后您可以在同一页面上多次循环数组:

foreach ($rows as $row) {
    print "<option value='" . $row['your_fieldname'] . "'>" . $row['your_fieldname'] . "</option>";
}

回答by Temidayo Dtuzzy Omotayo

After a while of research and disappointments....I was able to make this up

经过一段时间的研究和失望......我能够弥补这一点

     <?php $conn = new mysqli('hostname', 'username', 'password','dbname') or die ('Cannot connect to db') $result = $conn->query("select * from table");?>

//insert the below code in the body


    <table id="myTable"> <tr class="header"> <th style="width:20%;">Name</th>
    <th style="width:20%;">Email</th>
       <th style="width:10%;">City/ Region</th>
        <th style="width:30%;">Details</th>
  </tr>
  <?php
   while ($row = mysqli_fetch_array($result)) {

               echo "<tr>";
               echo "<td>".$row['username']."</td>";
               echo "<td>".$row['city']."</td>";
                echo "<td>".$row['details']."</td>";
               echo "</tr>";
           }

     ?>
</table>

Trust me it works :)

相信我它有效:)

回答by Bhupesh Shrestha

At the top first set up database connection as follow:

在顶部首先设置数据库连接如下:

<?php
$mysqli = new mysqli("localhost", "username", "password", "database") or die($this->mysqli->error);
$query= $mysqli->query("SELECT PcID from PC");
?> 

Then include the following code in HTML inside form

然后在表单内的 HTML 中包含以下代码

<select name="selected_pcid" id='selected_pcid'>

            <?php 

             while ($rows = $query->fetch_array(MYSQLI_ASSOC)) {
                        $value= $rows['id'];
                ?>
                 <option value="<?= $value?>"><?= $value?></option>
                <?php } ?>
             </select>

However, if you are using materialize css or any other out of the box css, make sure that select field is not hidden or disabled.

但是,如果您使用的是 materialize css 或任何其他开箱即用的 css,请确保选择字段未隐藏或禁用。

回答by rjose

What if you want to use both id and name in the dropdown? Here is the code for that:

如果您想在下拉列表中同时使用 id 和 name 怎么办?这是代码:

$mysqli = new mysqli($servername, $username, $password, $dbname);
$sqlSelect = "SELECT BrandID, BrandName FROM BrandMaster";
$result = $mysqli -> query ($sqlSelect);

echo "<select id='brandId' name='brandName'>";

while ($row = mysqli_fetch_array($result)) {
   unset($id, $name);
   $id = $row['BrandID'];
   $name = $row['BrandName']; 
   echo '<option value="'.$id.'">'.$name.'</option>';
 }
 echo "</select>";

回答by Gaetano Herman

No need to do this:

不需要这样做:

while ($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

You can directly do this:

你可以直接这样做:

while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['value'] . "'>" . $row['value'] . "</option>";
    }