我想用我的sql数据库中的名字填充我的组合框。我在W3schools上看到了一些代码,但我真的无法在代码中使用它。我的组合框填充了select
,但这不是我想要的那样。这是代码:
<?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);
?>
以下是我希望将其应用于我的代码。
<!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>
答案 0 :(得分:1)
以下是使用此代码进行一次组合框尝试的示例代码
<?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 :(得分:0)
试试这个简单的代码。它将为您提供有关如何使用MySQL和PHP创建内容的想法。
<?php
//your database connection code ...
echo "<select id='functie'>";
while($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['id']."'>".$row['column']."</option>";
}
echo "</select>";
//your additional following codes ...
?>
根据您的需要进行修改。