Sorry about the thread title, couldn’t think of anything else that was descriptive or helpful and the actual error message “Notice: undefined variable: blah blah blah” isn’t very helpful either as it’s so common!
Anyway…
I have a PHP page that simply displays some data from a MySQL DB. The PHP page takes a unique “slug” as a parameter in the URL to determine which data it should display. So, for example if the URL reads http://example.com/library/title/?slug=dracula then the PHP page displays the data corresponding to the Dracula book. If the URL reads http://example.com/library/title/?slug=a-christmas-carol then it display the data corresponding to the A Christmas Carol book.
Both pages work for the URL slug and displaying an image from a folder based on that slug, but only the Dracula page (Page A) displays the data from the MySQL DB. The A Christmas Carol (Page B) doesn’t display the data. Below are two screenshots showing the issue I’m having.
Dracula (Page A)
A Christmas Carol (Page B)
Here’s the code for index.php in the title folder:
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/test.com/includes/dbconnection.inc.php';
if (isset($_GET['slug']))
{
$slug = $_GET['slug'];
}
else
{
header('Location: .');
exit();
}
include $_SERVER['DOCUMENT_ROOT'] . '/test.com/includes/dbconnection.inc.php';
try
{
$sql = "SELECT bookTitle, authorName, publisherName, genreSubjectName
FROM books, authors, publishers, genresubject
WHERE books.bookID = authors.authorInfoID
AND books.bookID = publishers.publisherID
AND books.bookID = genresubject.genreSubjectID
AND bookSlugName = '$slug'";
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching title records: ' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $result->fetch())
{
$title = $row['bookTitle'];
$author = $row['authorName'];
$publisher = $row['publisherName'];
$genresubject = $row['genreSubjectName'];
}
include 'title.html.php';
?>
And here’s the basic code from the title.html.php file in the title folder:
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/test.com/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php htmlout($title); ?> – Huffier</title>
</head>
<body>
<article>
<header>
<h1><?php htmlout($title); ?></h1>
</header>
<div id="content">
<h2>Title Information</h2>
<div id="title-info-details">
<img src="../../images/covers/<?php htmlout($slug); ?>.png" alt="<?php htmlout($title); ?>" id="title-cover"/>
<ul id="title-info">
<li><strong>Author:</strong> <?php htmlout($author); ?></li>
<li><strong>Publisher:</strong> <?php htmlout($publisher); ?></li>
<li><strong>Pages:</strong> </li>
<li><strong>Genre / Subject:</strong> <?php htmlout($genresubject); ?></li>
<li><strong>Rating:</strong> </li>
<li><strong>ISBN:</strong> </li>
<li><strong>Price:</strong> £</li>
</ul>
</div>
<div id="title-info-ctas">
<div class="title-cta">
<p class="top"><a href="../read/?slug=<?php htmlout($slug); ?>" class="title-cta-link">Read On-Demand</a></p>
</div>
<div class="title-cta">
<p><a href="../buy/?slug=<?php htmlout($slug); ?>" class="title-cta-link">Buy eBook Bundle</a></p>
</div>
</div>
<div id="title-desc">
<h2>Title Description</h2>
<p>eBook description.</p>
</div>
</div>
</article>
</body>
</html>
I’m really stumped, honestly, as to why it works perfectly fine for the Dracula page but not for the Christmas Carol page. I’m sure it’s a problem with the PHP code I’ve used. I initially had it displaying as a foreach loop with an array and that didn’t work so I changed it to a while loop. Still doesn’t work properly.
Could anyone point me in the right direction of what I should be searching for on Google? Or at best provide a solution for me with an explanation as to why what I’ve currently got doesn’t work in this situation? Very much appreciated. Been stumped on it for about a week now trying to figure it out…I know it’s going to be something easy to fix…For somebody who knows PHP, unlike me. This is really my first proper PHP & MySQL project.