编程语言
首页 > 编程语言> > PHP搜索引擎注意:未定义索引:类别

PHP搜索引擎注意:未定义索引:类别

作者:互联网

这个问题已经在这里有了答案:            >            “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP                                    28个
>            PHP search function in a category [closed]                                    2个
大家好,我在一些类别的帮助下制作了一个脚本
示例:我们有下一个PCgames,全部和文档. X用户正在PCgames中搜索Y游戏.该脚本在PCgames游戏中用于Y游戏,并显示所有结果.
但是,当用户单击搜索按钮时,服务器将显示下一个错误注意:未定义的索引:第5行中search.php中的类别
HTML代码

    <form action="search.php" method="POST">
<p><br />
<input name="q" type="hidden" />
<font color="black">Hunt Data:</font>
<input type="text" style="width: 180px" name="qfront" id="name" />
<select name="category">
<option value="0" id="all">All</option>
<option value="1" id="PCgames">PC-Games</option>
<option value="2" id="Console">Console</option>
<option value="3" id="Movies">Movies</option>
<option value="4" id="Music">Music</option>
<option value="5" id="XXX">XXX</option>
<option value="6" id="Windows">Windows</option>
<option value="7" id="Linux">Linux</option>
<option value="8" id="Software">Software</option>
<option value="9" id="Documents">Documents</option>
</select>
<input type="submit" value="Search" />
</p>
</form>

search.php文件

    <?php
//define each directory here, the index starts with 0 == all and so on.
$categorydir = array('/Category/All/', '/Category/PCGames/', '/Category/Documents/');
//if option selected and its valid number
if($_POST['category'] && ctype_digit($_POST['category'])){    //line 5
 if(array_key_exists($_POST['category'], $categorydir) && is_dir($categorydir[$_POST['category']])){
   $handle = opendir($categorydir[$_POST['category']]);
 }else{
   echo 'target directory not found';
 }
}else{
 //please enter an option
}
?>

注意事项的一些帮助:未定义的索引:第5行的search.php中的category?

解决方法:

if(isset($_POST['category']) {
 if(ctype_digit($_POST['category']) && isset($categorydir[$_POST['category']])){

 }
}

和你的数组

 $categorydir = array('/Category/All/', '/Category/PCGames/', '/Category/Documents/');

有3个条目,尽管您的表格有10只猫.

标签:php,search-engine
来源: https://codeday.me/bug/20191013/1904406.html