首先,您需要创建一个输入数组,而不是“ val1”,“ val2”.
只需给每个输入一个val [x]的名称,例如val [1],val [2]等.或者,您可以让它成为一个新的数组元素:val [].
然后,您需要在构建查询之前使用PHP foreach处理此问题,例如:
$sql_query = "SELECT info From Products";
$conditions = array();
if (isset($_GET['val']) && is_array($_GET['val'])) {
$vals = $_GET['val'];
foreach ($vals as $key => $val) {
$conditions[] = '`field'. $key .'` = '. $val;
// You should change the conditions the way you want, I'm not sure about what you want here.
}
}
if (count($conditions) > 0) {
$sql_query .= ' where '. implode(' and ', $conditions);
}
您完成了!
更新
如评论中所述,这是您要寻找的条件:
foreach ($vals as $val) {
$conditions[] = '`item_id` = '. $val;
}
此外,在这种情况下,请不要忘记您是否需要AND.因此,也请编辑此行:
$sql_query .= ' where '. implode(' or ', $conditions);