首页 > 解决方案 > 从表中选择值!=数组中的值

问题描述

从 1 到 10 的数组我想进行下拉以选择数组中排除数据库中表中的值的值 ex:id=3 在表中==>下拉选择(1,2,4,5, 6,7,8,9,10) ex:id=4 在表中 ==>下拉选择(1,2,3,5,6,7,8,9,10)

<select name="Alert_Severtiy_No" class="form-control">
<option value="">select</option>
 $no=array(1,2,3,4,5,6,7,8,9,10);

foreach($no as $key):
  $hh="select * from Alert_Severtiy where Alert_Severtiy_No Not <>$no";
  $stmt = sqlsrv_query($connect, $hh);

echo '<option value="'.$hh.'">'.$hh.'</option>'; 
endforeach;
echo'

</select>

标签: phparraysselectoption

解决方案


在您的查询中,您需要使用 $key 而不是 $no

$hh="select * from Alert_Severtiy where Alert_Severtiy_No Not <>$key";

但我建议您一次从数据库中获取所有结果,而不是每次运行循环时都查询数据库。

您可以执行以下操作:

$here = implode(",", $no);
$hh="select * from Alert_Severtiy where Alert_Severtiy_No Not IN ($here)"; 

然后循环遍历返回的结果。


推荐阅读