0

I am making a website that is linked to a database. But I came across a problem that i cant seem to solve. I want to assign a background to a page when the ID in the URL is a certain number. So I made a Query:

<?php

require 'app/start.php';

$pages = $db->query("
    SELECT id, name
    FROM sprints
")->fetchAll(PDO::FETCH_ASSOC);

$vakid = $_GET["vak"];

$bg = $db->query("
    SELECT background
    FROM categories
    WHERE id = $vakid
")->fetchAll(PDO::FETCH_ASSOC);

require VIEW_ROOT . '/sprint.php';

The last query is the one that should take the background path! I included the other query for the page too, maybe it can help! I get the ID from the URL with $_GET.

I tested this query and it works in my database.. but when i include it in my code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">


        <link rel="stylesheet" href="<?php echo BASE_URL?>/CSS/CMSTutorial.css">
    </head>
<body>
    <div id="sprintwrap" style="background-image: url('<?php echo e($bg['background']);?>')"></div>
    <div id="sprintbg">

        <?php if (empty($pages)): ?>
            <p>Sorry, no pages at the moment.</p>
        <?php else: ?>
            <ul>
                <?php foreach ($pages as $page): ?>
                    <li><a href="<?php echo BASE_URL; ?>/vak.php?vak=<?php echo $vakid ?>&sprint=<?php echo $page['id']; ?>"><?php echo $page['name']; ?></a></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>

    </div>
</body>

It doesn't work. I don't know what to do :( I hope you guys can help me! Thanks in advance!

This is the error: Undefined index: background in /// on line 10 (the html file I included, sprintwrap div)

  • You use fetchAll. But this returns not a single row, but a list of rows. So, the variable BG is not a single result row, but a list of rows. Use fetch instead of fetchAll in this case when you just want the first row. – NineBerry Jan 06 '18 at 21:11
  • Note that including the query parameter into the SQL string directly, makes SQL injection possible. Don't do that! Use a prepared statement instead! – NineBerry Jan 06 '18 at 21:12

0 Answers0