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

如何在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>

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>

Try this simple code. It will give you an idea on how you should create something with MySQL and 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 ...
    ?>

Modify it to suit your needs.