简体   繁体   English

在 html 选择选项中显示来自 sql 的值不显示值

[英]displaying values from sql in html select option not displaying values

i have created a html form using php and sql.我已经使用 php 和 sql 创建了一个 html 表单。 The form contains a field called spaces which is a select dropdown.该表单包含一个名为空格的字段,它是一个选择下拉列表。 The values are coming from an array which i declared in php.这些值来自我在 php 中声明的数组。 If the values in array are already present in database it should not display.如果数组中的值已经存在于数据库中,则不应显示。 So i have done the following code:所以我做了以下代码:

 <select class="form-control" id="space" name="space"> <option value="--Select--">--Select--</option> <?php $select=mysqli_query($con,"select * from clients where Spaces IS NOT NULL"); while($menu1=mysqli_fetch_array($select)) { $filled =$menu1['name']; $valuez = array("C101","C102","C103","C104"); if ($filled != $valuez) { ?> <option value="<?php echo $valuez;?>"> <?php echo $valuez;?> </option> <?php }} ?> </select>

but this is not making any values display.但这并没有显示任何值。 Can anyone please tell me what is wrong in my code.谁能告诉我我的代码有什么问题。 thanks n advance提前谢谢

Your $filled is string, cannot equals to array.你的$filled是字符串,不能等于数组。

And you can just use whereIn , don't need to take all clients out:你可以只使用whereIn ,不需要把所有的客户都拿出来:

<select class="form-control" id="space" name="space">
  <option value="--Select--">--Select--</option>
  <?php
    $valuez = "'C101','C102','C103','C104'";
    $select = mysqli_query($con, "SELECT * FROM clients WHERE Space IN ($valuez)");
  ?>

<?php while($menu1 = mysqli_fetch_array($select) ) : ?>
    <option value="<?php echo $menu1['name'];?>">
      <?php echo $menu1['name'];?>
    </option>
<?php endwhile; ?> 
</select>

you are comparing a string with the array.您正在将字符串与数组进行比较。 you should use in_array like this你应该像这样使用 in_array

 <select class="form-control" id="space" name="space">
   <option value="--Select--">--Select--</option>
 <?php
  $select=mysqli_query($con,"select * from clients where Space IS NOT NULL");
 while($menu1=mysqli_fetch_array($select))
  {
  $filled =$menu1['Space'];
 $valuez = array("C101","C102","C103","C104");
 foreach($valuez as $value){
    if($value != $filled){ 
    ?>
        <option value="<?php echo $value;?>">
          <?php echo $value;?>
        </option>
    <?php 
    }
 }
}
?>

update the code更新代码

Try this and please make sure the value you want to display as the options are from the column name or spaces.试试这个,请确保您要显示的值作为选项来自列名或空格。 I used the name($menu1['name']) column as per your code.我根据您的代码使用了 name($menu1['name']) 列。

<select class="form-control" id="space" name="space">
  <option value="--Select--">--Select--</option>
   <?php
     $select = mysqli_query($con,"select * from clients where Spaces NOT IN ('C101','C102','C103','C104')");
     while($menu1=mysqli_fetch_array($select)) {
       $filled = $menu1['name'];
       if (!empty($filled)) {
   ?>
   <option value="<?php echo $filled;?>">
     <?php echo $filled;?>
   </option>
  <?php
    }}
  ?>
</select>

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

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