douwen5833 2016-02-09 09:24
浏览 41
已采纳

如何在PHP中填充我的组合框

I want to fill my combobox with names from my sql database. I saw some code on W3schools, but I really can't get it to work in my code. I have the combobox filled with select but that's not what I want it to be like. Here is the code:

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Here is my code where I want it to apply to.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="javascript.js">
<link rel="stylesheet" href="layout.css">
<title>Bestuur wijzigen</title>
<link rel="icon" href="images/favicon.png">
</head>
<body>
<ul class="horizontal gray">
<li><a class="active" href="index.php">Bestuur</a></li>
<li><a href="bestuurWijzigen.php">Bestuur wijzigen</a></li>
<li><a href="bestuurToevoegen.php">Bestuur toevoegen</a></li>
</ul>
<form action="index.php">
<table class="table" border="1" frame="void" rules="rows">
<tr>
    <td><label for="naam">Kies een bestuurslid</label></td>
    <td>
        <select>
        <option value="volvo">Luca Fraser</option>
        <option value="saab">Pieter Schreurs</option>
        <option value="opel">Wessel Oblink</option>
        <option value="audi">Andre Lammers</option>
    </select>
    </td>
</tr>
<tr>
    <td><label for="functie">Functie</label></td>
    <td>
        <select>
        <option value="#" selected="">Voorzitter</option>
        <option value="#">Secretaris</option>
        <option value="#">Penningmeester</option>
    </select>
    </td>
</tr>

<tr>
    <td><button type="submit" class="button">Opslaan</button</td>
</tr>
</tbody></table>
</form>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • duanhuang3074 2016-02-09 09:40
    关注

    Here is sample code for one combobox try with this code

        <?php
        $q = intval($_GET['q']);
    
        $con = mysqli_connect('localhost','peter','abc123','my_db');
        if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
        }
    
        mysqli_select_db($con,"ajax_demo");
        //$sql="SELECT * FROM user WHERE id = '".$q."'"; //your old code
        $sql = "SELECT * FROM user WHERE id LIKE = '%$q%'";
        $result = mysqli_query($con,$sql);
    
        $name = [];
        while($row = mysqli_fetch_array($result)) {
        $names[] = $row['FirstName'];  
    
        }
        mysqli_close($con);
        ?>
    //if your query not working uncomment following line and check then it is working the problem is your query result
    // $names = ['jhon','patel','Ameer','Stepan'];  
         <select>
    <?php foreach($names as $name){  ?>
           <option value="#"><?php echo $name; ?></option>
    <?php } ?>
         </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word