提问人:Taccoman 提问时间:8/27/2018 更新时间:8/27/2018 访问量:203
php 中未定义的索引错误(下拉过滤器不起作用)
Undefined index error in php (dropdown filters do not work)
问:
嘿,我搜索了很多关于这个问题的信息,但我尝试的所有方法都没有奏效。:(
我的代码有 3 个下拉列表,它们应该像搜索过滤器一样工作,但每次我从下拉列表中选择一个选项时,我都会收到 3 行的未合并索引错误,如果我更改另一个下拉列表,其他两个过滤器将被忽略...... :(
这只是第一个下拉列表的代码:
<?php
//when the filter changes, this php is called
$output = '';
if(isset($_POST["businessUnit"]))
{
if($_POST["businessUnit"] != '')
{
if($_POST["productGroup"] != '') //first undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit = '".$_POST["businessUnit"]."' and productGroup = '".$_POST["productGroup"]."'";
}
else if($_POST["deviceType"] != '') //second undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit =
'".$_POST["businessUnit"]."' and productGroup = '".$_POST["deviceType"]."'";
}
else if($_POST["productGroup"] != '' && $_POST["deviceType"] != '') //third undefined index error
{
$sql = "SELECT * FROM item WHERE businessUnit = '".$_POST["businessUnit"]."' and productGroup = '".$_POST["productGroup"]."' and deviceType = '".$_POST["deviceType"]."'";
}
else
{
$sql = "SELECT * FROM item WHERE businessUnit = '".$_POST["businessUnit"]."'";
}
}
else
{
$sql = "SELECT * FROM item";
}
$result = sqlsrv_query($connect, $sql);
while($row = sqlsrv_fetch_array($result))
{
$output .= "<tr><td>".
$row['businessUnit']."</td><td>".
$row['productGroup']."</td><td>".
$row['deviceType']."</td><td>".
$row['serialNumber']."</td><td>".
$row['location']."</td><td>".
$row['condition']."</td><td>".
$row['itemDescription']."</td><td>
<input type='checkbox'></input></td></tr>";
}
echo $output;
}
else{
$_POST["businessUnit"] = "";
}
?>
答:
1赞
Parag Soni
8/27/2018
#1
要处理错误,将不起作用,您必须先使用不等于空白检查。
请参阅下面的解决方案,它可能有效。undefined index
$array[$key] != ""
isset()
if (isset($_POST["businessUnit"]))
{
if ($_POST["businessUnit"] != '')
{
if (isset($_POST["productGroup"]) && $_POST["productGroup"] != '') //first undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit = '" . $_POST["businessUnit"] . "' and productGroup = '" . $_POST["productGroup"] . "'";
} else if (isset($_POST["deviceType"]) && $_POST["deviceType"] != '') //second undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit =
'" . $_POST["businessUnit"] . "' and productGroup = '" . $_POST["deviceType"] . "'";
} else if (isset($_POST["productGroup"]) && $_POST["productGroup"] != '' && isset($_POST["deviceType"]) && $_POST["deviceType"] != '') //third undefined index error
{
$sql = "SELECT * FROM item WHERE businessUnit = '" . $_POST["businessUnit"] . "' and productGroup = '" . $_POST["productGroup"] . "' and deviceType = '" . $_POST["deviceType"] . "'";
} else
{
$sql = "SELECT * FROM item WHERE businessUnit = '" . $_POST["businessUnit"] . "'";
}
} else
{
$sql = "SELECT * FROM item";
}
$result = sqlsrv_query($connect, $sql);
while ($row = sqlsrv_fetch_array($result))
{
$output .= "<tr><td>" .
$row['businessUnit'] . "</td><td>" .
$row['productGroup'] . "</td><td>" .
$row['deviceType'] . "</td><td>" .
$row['serialNumber'] . "</td><td>" .
$row['location'] . "</td><td>" .
$row['condition'] . "</td><td>" .
$row['itemDescription'] . "</td><td>
<input type='checkbox'></input></td></tr>";
}
echo $output;
} else
{
$_POST["businessUnit"] = "";
}
}
0赞
Pawan lakhera
8/27/2018
#2
我不明白你想做什么。请发布显示快照并明确说明您要做什么。你可以写:
echo "<pre>";print_r($_POST);
after $output = '';
看看 $_POST 变量中有什么,如果你没有找到,那就是:
$_POST["productGroup"]
在 $_POST 中,您将获得未定义的索引。因此,请确保在 $_POST 请求中传递它。
评论
0赞
Taccoman
8/27/2018
很抱歉,如果我不清楚,感谢您的帮助,但@Parag Soni 已经为我的问题提供了完美的答案。:)
评论