从表中选择值!=数组中的值

时间:2018-08-19 09:41:24

标签: php arrays select option

数组从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>

1 个答案:

答案 0 :(得分:2)

在查询中,您需要使用$ 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)"; 

然后遍历返回的结果。