php 如何修复“注意未定义索引:”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39791263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix 'Notice Undefined index:'
提问by Sohan Sonar
I received the following error message when I go on searchproduct page. How may i fixed it?
当我进入搜索产品页面时收到以下错误消息。我该如何修复它?
Notice: Undefined index: dosageForm in C:\xampp\htdocs\prism\searchproduct.php on line 29
注意:未定义索引:doesForm in C:\xampp\htdocs\prism\searchproduct.php 第 29 行
Notice: Undefined index: dosageForm in C:\xampp\htdocs\prism\searchproduct.php on line 32
注意:未定义索引:doesForm in C:\xampp\htdocs\prism\searchproduct.php 第 32 行
Notice: Undefined index: strengths in C:\xampp\htdocs\prism\searchproduct.php on line 35
注意:未定义索引:第 35 行 C:\xampp\htdocs\prism\searchproduct.php 中的强度
Notice: Undefined index: strengths in C:\xampp\htdocs\prism\searchproduct.php on line 38
注意:未定义索引:第 38 行 C:\xampp\htdocs\prism\searchproduct.php 中的强度
if($_POST['dataType']!= "") {
$dataType = $_POST['dataType'];
}else{
$dataType = $_GET['dataType'];
}
if($_POST['drugCategory']!= ""){
$drugCategory = $_POST['drugCategory'];
}else{
$drugCategory = $_GET['drugCategory'];
}
if($_POST['productName']!= ""){
$productName = $_POST['productName'];
}else{
$productName = $_GET['productName'];
}
if($_POST['brandName']!= ""){
$brandName = $_POST['brandName'];
}else{
$brandName = $_GET['brandName'];
}
if($_POST['dosageForm']!= ""){
$dosageForm = $_POST['dosageForm'];
}else{
$dosageForm = $_GET['dosageForm'];
}
if($_POST['strengths']!= ""){
$strengths = $_POST['strengths'];
}else{
$strengths = $_GET['strengths'];
}
i am getting same type of error ,
我收到相同类型的错误,
please help me to resolve
请帮我解决
回答by Gectou4
use isset()
用 isset()
if(isset($_POST['drugCategory']))
or in your case : empty()
或者在你的情况下: empty()
if(!empty($_POST['drugCategory']))
Update:
更新:
PHP7.1 allow this one too but it will may be forbidden by CodeStyle's rules
PHP7.1 也允许这个,但它可能被 CodeStyle 的规则禁止
if($_POST['drugCategory'] ?? false)