简体   繁体   English

从 PHP 中的 mySQL 表填充下拉框

[英]Populate a Drop down box from a mySQL table in PHP

I am trying to populate a Drop down box from results of a mySQL Query, in Php.我试图在 PHP 中从 mySQL 查询的结果填充下拉框。 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 :)任何帮助都会很棒:)

Here's my Query: Select PcID from PC;这是我的查询: Select PcID from PC;

You will need to make sure that if you're using a test environment like WAMP set your username as root.您需要确保如果您使用的是 WAMP 之类的测试环境,请将您的用户名设置为 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.这是一个连接到 MySQL 数据库、发出查询并从表中的每一行输出<select>框的<option>标记的示例。

<?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>";

?>

Below is the code for drop down using MySql and PHP :下面是使用MySqlPHP进行下拉的代码:

<?
$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>";
?>

Since mysql_connect has 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>";
}

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 :)相信我它有效:)

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>";
    }

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,请确保选择字段未隐藏或禁用。

What if you want to use both id and name in the dropdown?如果您想在下拉列表中同时使用 id 和 name 怎么办? Here is the code for that:这是代码:

$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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM