php - 尝试访问值类型为 null 的数组偏移量
问题描述
我已将我的 PHP 版本更新为 7.4,之后开始出现此错误。当我尝试在页面刷新后维护数据时,错误显示在选项中,在 <selected 标记中。
<?PHP
$posts = filter_input_array(INPUT_POST, FILTER_DEFAULT);
?>
<form method="post" name="create_posts">
<label>Categories available</label>
<select name="category_id" class="select_list">
<option value="null">Pick a category: </option>
<?PHP
$readCategs= new Read();
$readCategs->query("SELECT DISTINCT ca.id AS cat_id,
ca.title AS title, ca.content AS content
FROM categories ca LEFT JOIN posts p ON p.cat_id = ca.id
ORDER BY title ASC");
//var_dump($readCategs);
if(!$readCategs->getResult()):
echo "<a href='create_category'><option disabled='disabled'>
There are no categories</option></a>";
else:
foreach($readCategs->getResult() AS $categs):
echo "<option value=\"{$categs['cat_id']}\" class='options' ";
if($posts['category_id'] == $categs['cat_id']):
echo 'selected="selected" ';
endif;
echo ">{$categs['title']}</option>";
endforeach;
endif;
?>
</select>
</form>````
The error shows on the "if($posts['category_id'] == $categs['cat_id']):" line
解决方案
倾向于建议 $posts 或 $categs 为空或不是数组。尝试以下以显示哪个不是数组
var_dump($posts); var_dump($categs);
您可以使用 is_array 测试数组
推荐阅读
- mysql - windows server 2012 mysql日志文件已删除但释放c中的空间
- vuejs2 - 龙卷风阻止其他请求或 ioloop
- python - 如何将对话框输入的返回值返回到 PyQt5 中的另一个函数?
- angular - 类型“数字”不可分配给类型“布尔”
- java - 当 Firebase 中的值发生更改时,Sharedprefrences 值会更新
- go - 一种处理不同数据类型的方法
- python - 如何获取整个 json 主体作为用户输入并存储在变量中
- xml - Perl 删除重复的 XML 标签
- java - 如何在 Spring security 5.0 中自定义令牌响应格式
- bash - 什么 export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[ \033[m\]\$ " 在 MacOS 的 bash 终端中是什么意思?