php - 从表中选择值!=数组中的值
问题描述
从 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>
解决方案
在您的查询中,您需要使用 $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)";
然后循环遍历返回的结果。
推荐阅读
- java - Java JSlider:方法 setLabelTable 不推荐使用字典,我还应该使用什么?
- java - Java使用单独的任务异步同步两个集合中的项目
- html - 文本图像响应
- javascript - 滚动在 Firefox 中不起作用,我有一个表格,当用户向下滚动时会加载更多,但它会在 Firefox 中停止
- powerbi - 日期间隔中的剩余工作日
- firebase - 不使用拉刷新进行更新,而是重复旧数据
- c++ - 如何动态添加模板参数
- python-3.x - 什么是 ray::IDLE,为什么有些工作人员内存不足?
- laravel - Laravel - 如何验证请求规则中另一个表的字段
- java - 上传多个文件时HTTP/2是否支持多路复用