选择列表回显多次而不是一次

时间:2017-03-03 10:40:53

标签: php

我有一个选择列表,在选中时,回显一个带有另一个选择列表的表单。但是,它不是向我显示一个选择列表,而是显示两次相同的选择列表。谁知道为什么会这样?

选择列表:

echo '<div class="form-group col-xs-12 col-md-12">';
echo '<label for="selectlist">Lend devices:</label>';
echo    '<select name="selectlist" id="selectlist" class="selectpicker form-control" data-live-search="true" onchange="GrabRegDevice(this.value);">';
echo        '<option value="" disabled selected>Name devices</option>';
$db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
if (!$db) {
    exit('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
$querylenddevices = "SELECT * FROM register WHERE orgnaam = '" . $q . "'";
$db = mysqli_query($db,$querylenddevices);
while ( $d=mysqli_fetch_assoc($db)) {
    echo "<option value='".$d['devicename']."'>".$d['devicename']."</option>";
}
echo    '</select>';
echo '</div>';

修改 这是整个.PHP文件

<?php require 'database.php';

$q=$_GET["q"];    
$sql = "SELECT DISTINCT * FROM register WHERE orgnaam = '" . $q . "'";  
$result = mysqli_query($db,$sql);

while($row = mysqli_fetch_array($result)) {
    echo '<div class="form-group col-xs-12 col-md-12">';
    echo    '<label for="naam">Organisatie naam:</label>';
    echo    '<input type="" class="form-control" readonly id="orgnaam" name="orgnaam" value="' . $row['orgnaam'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-6">';
    echo    '<label for="adres">Organisatie adres:</label>';
    echo    '<input type="" class="form-control" readonly id="adres" name="adres" value="' . $row['orgadres'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-6">';
    echo    '<label for="postcode">Organisatie postcode:</label>';
    echo    '<input type="" class="form-control" readonly id="postcode" name="postcode" value="' . $row['orgpostcode'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-6">';
    echo    '<label for="woonplaats">Organisatie woonplaats:</label>';
    echo    '<input type="" class="form-control" readonly id="woonplaats" name="woonplaats" value="' . $row['orgwoonplaats'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-6">';
    echo    '<label for="telnummer">Organisatie telefoonnummer:</label>';
    echo    '<input type="" class="form-control" readonly id="telnummer" name="telnummer" value="' . $row['orgtel'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-4">';
    echo    '<label for="">Naam contactpersoon:</label>';
    echo    '<input type="naamcontact" class="form-control" readonly id="naamcontact" name="naamcontact" value="' . $row['naamcontact'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-4">';
    echo    '<label for="emailcontact">Email contactpersoon:</label>';
    echo    '<input type="" class="form-control" readonly id="emailcontact" name="emailcontact" value="' . $row['emailcontact'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-4">';
    echo    '<label for="telcontact">Telefoonnummer contactpersoon:</label>';
    echo    '<input type="" class="form-control" readonly id="telcontact" name="telcontact" value="' . $row['telcontact'] . '">';
    echo '</div>';

    echo '<div class="form-group col-xs-12 col-md-12">';
    echo '<label for="selectlist">Lend devices:</label>';
    echo    '<select name="selectlist" id="selectlist" class="selectpicker form-control" data-live-search="true" onchange="GrabRegDevice(this.value);">';
    echo        '<option value="" disabled selected>Name devices</option>';
    $db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
    if (!$db) {
        exit('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
    }
    $querylenddevices = "SELECT * FROM register WHERE orgnaam = '" . $q . "'";
    $db = mysqli_query($db,$querylenddevices);
    while ( $d=mysqli_fetch_assoc($db)) {
        echo "<option value='".$d['devicename']."'>".$d['devicename']."</option>";
    }
    echo    '</select>';
    echo '</div>';
}

编辑2 在数据库中注册&#39;包含例如3个设备,它显示3倍的选择列表。这可能是因为我没有在期权价值中使用ID吗?

1 个答案:

答案 0 :(得分:0)

问题是整个选择列表是在while循环中设置的。因此,对于每个设备名称,它会生成另一个选择列表。现在我在while循环之外回显select-list,只放入while循环。这是我的代码:

<?php require 'database.php';


$q=$_GET["q"];

$sql = "SELECT DISTINCT * FROM register WHERE orgnaam = '" . $q . "'";

$result = mysqli_query($db,$sql);

while($row = mysqli_fetch_array($result)) {

echo '<div class="form-group col-xs-12 col-md-12">';
echo    '<label for="naam">Organisatie naam:</label>';
echo    '<input type="" class="form-control" readonly id="orgnaam" name="orgnaam" value="' . $row['orgnaam'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-6">';
echo    '<label for="adres">Organisatie adres:</label>';
echo    '<input type="" class="form-control" readonly id="adres" name="adres" value="' . $row['orgadres'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-6">';
echo    '<label for="postcode">Organisatie postcode:</label>';
echo    '<input type="" class="form-control" readonly id="postcode" name="postcode" value="' . $row['orgpostcode'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-6">';
echo    '<label for="woonplaats">Organisatie woonplaats:</label>';
echo    '<input type="" class="form-control" readonly id="woonplaats" name="woonplaats" value="' . $row['orgwoonplaats'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-6">';
echo    '<label for="telnummer">Organisatie telefoonnummer:</label>';
echo    '<input type="" class="form-control" readonly id="telnummer" name="telnummer" value="' . $row['orgtel'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-4">';
echo    '<label for="">Naam contactpersoon:</label>';
echo    '<input type="naamcontact" class="form-control" readonly id="naamcontact" name="naamcontact" value="' . $row['naamcontact'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-4">';
echo    '<label for="emailcontact">Email contactpersoon:</label>';
echo    '<input type="" class="form-control" readonly id="emailcontact" name="emailcontact" value="' . $row['emailcontact'] . '">';
echo '</div>';

echo '<div class="form-group col-xs-12 col-md-4">';
echo    '<label for="telcontact">Telefoonnummer contactpersoon:</label>';
echo    '<input type="" class="form-control" readonly id="telcontact" name="telcontact" value="' . $row['telcontact'] . '">';
echo '</div>';

}
echo '<div class="form-group col-xs-12 col-md-12">';
        $querytest = "SELECT * FROM register WHERE orgnaam = '" . $q . "'";
        $result = mysqli_query($db, $querytest);
echo    '<label for="selectlist">Geleende apparaten:</label>';
echo        '<select name="selectlist" id="selectlist" class="selectpicker form-control" data-live-search="true" onchange="GrabRegDevice(this.value);">';
echo            '<option value="" disabled selected>Naam apparaat</option>';
                    while($rowtest = mysqli_fetch_array($result)) {
                        echo "<option value='" . $rowtest['apparaatnaam'] . "'>" . $rowtest['apparaatnaam'] . "</option>";
                    }
echo        '</select>';
echo '</div>';

?>