I am currently in the process of building a simple forum for a small project. I have a page called forum.php. This page contains the contents that a person would see once they are logged. This page contains the following code:
<?php
include ('databaseconnect.php');
$query = "select * from Categories";
$result = mysqli_query($db,$query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3><a href="category.php?id">' . $row['Categoryname'] .
'</a></h3>' . $row['Categorydescription'];
echo '</td>';
echo '<td class="rightpart">';
echo '<a href="topic.php?id=">Topic subject</a> at 10-10';
echo '</td>';
echo '</tr>';
}
$result = $db->query($query);
?>
When a person clicks on any of the category links for example 'Community Events', it takes them to a page called category.php. Now, on this page I would like them to see an overview of the topics created for a specific category. So the following code was used:
<?php
include ('databaseconnect.php');
$sql1= "SELECT Categoryid, Categoryname, Categorydescription
FROM Categories
WHERE Categoryid= '" . mysqli_real_escape_string($db,$_GET['id'])."'";
$result1= mysqli_query($db,$sql1);
if(!$result1)
{
echo "<font color = 'Red' .<p> Category Cannot be displayed, Contact the
administrator </p> </font>". mysqli_error($db);
}
if(mysqli_num_rows($result1) == 0)
{
echo "<font color = 'Red' .<p>This category does not exist.</p> </font>".
mysqli_error($db);
}
while($row = mysqli_fetch_assoc($result1))
{
echo '<h2>Topics in ′' . $row['Categoryname'] . '′ Category</h2>';
}
?>
So the specific problem I am experiencing is when I click on any of the links I am getting the error that I defined which is:
'This category does not exist'
So, I tried changing the id of the categories.php page to 1 and I got the following result:
But when I went and click on the Health and Fitness category instead of saying 'Topics in 'Health and Fitness' Category it still says 'Topics in Community Events Category'. So I changed the id to 2 and the opposite resulted whereby it says 'Topics in 'Health and Fitness' Category on both pages. So is the solution I have to make the id's dynamic or is there a syntactic error. Your help would be greatly appreciated.