Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);


This is the result of the code above: [^]


When a person clicks on any 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 as well as. 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 was experiencing is when I click on any of the links in the categories table 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 which is the following:
PHP
<h3><a href="category.php?id=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 as the HTML header it still says 'Topics in Community Events Category'.



So I changed the "id"of the categories.php page to 2 which is the following:
PHP
<h3><a href="category.php?id=2"
and the opposite resulted whereby it says 'Topics in 'Health and Fitness' Category on both pages. This is the result:
[^]

Is there any other way that this problem can be fixed?

What I have tried:

1) Changing the id to from

<a href="category.php?id=" to

<a href="category.php?id=1" and

<a href="category.php?id=1"


2) Changing this $sql1= "SELECT Categoryid, Categoryname, Categorydescription
FROM Categories
WHERE Categoryid= '" . mysqli_real_escape_string($db,$_GET['id'])."'";

to $sql1= "Select * from Categories"

3) Following some of the step found here:

https://code.tutsplus.com/tutorials/how-to-create-a-phpmysql-powered-forum-from-scratch--net-10188

Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900