I keep getting this error when im trying to show category name.

Joined
Nov 21, 2023
Messages
3
Reaction score
0
i keep getting this error message:

Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\index.php on line 33

Fatal error: Uncaught TypeError: htmlspecialchars(): Argument #1 ($string) must be of type string, null given in C:\xampp\htdocs\index.php:33 Stack trace: #0 C:\xampp\htdocs\index.php(33): htmlspecialchars(NULL) #1 {main} thrown in C:\xampp\htdocs\index.php on line 33.

Here is the code:
<?php
declare(strict_types = 1);
require 'includes/connection.inc.php';
require 'includes/Functions.inc.php';
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$sql = "SELECT id, name, description FROM category WHERE id=:id;";
$category = pdo($pdo, $sql, [$id])->fetch();
$sql = "SELECT a.id, a.title, a.summary, a.category_id, a.member_id,
c.name AS category,
CONCAT(m.forename, ' ', m.surname) AS author,
i.file AS image_file,
i.alt AS image_alt
FROM article AS a
JOIN category AS c ON a.category_id = c.id
JOIN member AS m ON a.member_id = m.id
LEFT JOIN image AS i ON a.image_id = i.id
WHERE a.category_id = :id AND a.published = 1
ORDER BY a.id DESC;";
$articles = pdo($pdo, $sql, [$id])->fetchAll();
$title = $category['name'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
//this is where the error is occurring <h1><?= htmlspecialchars($category['name']) ?></h1>
<?php foreach ($articles as $article) { ?>
<article class="summary">
<a href="article.php?id=1<?= $article['id'] ?>">
<img src="uploads/<?= htmlspecialchars($article['image_file']) ?>" alt="<?= htmlspecialchars($article['image_alt']) ?>" >
<h2><?= htmlspecialchars($article['title']) ?></h2>
<p><?= htmlspecialchars($article['summary']) ?></p>
</a>
<p class="credit">
Posted in <a href="category.php?id=<?= $article['category_id'] ?>">
<?= htmlspecialchars($article['category']) ?></a>
by <a href="member.php?id=<?= $article['member_id'] ?>">
<?= htmlspecialchars($article['author']) ?></a>
</p>
<?php } ?>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top