Trying to access array offset on value type of null on a

Viewed 52

I've updated my PHP version to 7.4 and this error started showing afterwards. The error shows in the options, in <selected tag, when I try to maintain data even after the page has been refreshed.

<?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
1 Answers

Tends to suggest either $posts or $categs is null or not an array. Try the follow to show which is not an array var_dump($posts); var_dump($categs);

You can test for an array with is_array

Related