How to do a forum increment

I am trying to get the following forum to work but can’t get the view to increment and I also have got a boolean error with my $row4

<?php
include_once __DIR__.'/header2.php';
if(!$_SESSION['u_uid']) {
	echo "<meta http-equiv='refresh' content='0;url=index.php?display_music_forum_topics=notlogin'>"; 
    exit();
} else {
	include_once __DIR__.'/includes/dbh.php';
	
		
		$cat = strip_tags($_GET['cat']);
		$topic_id = strip_tags($_GET['topic_id']);
        $admin_url = strip_tags($_GET['admin']);
      //  $limit = 1;
		$sql = "SELECT * FROM music_forum_sub_cats WHERE cat_id = ?;";

		$stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql)) {
			           echo "SQL error";
			        } else {
			          mysqli_stmt_bind_param($stmt, "i", $cat);
			          mysqli_stmt_execute($stmt);
			          $result = mysqli_stmt_get_result($stmt);
			          $resultCheck = mysqli_num_rows($result);
			          $row = mysqli_fetch_assoc($result);
			         
			          
			          if ($resultCheck < 1) {
			             	echo "<meta http-equiv='refresh' content='0;url=header2.php?display_music_forum_topics=nocatrecords'>"; 
                            exit(); 
			          } else {
			            
			        
                         
			           echo '<table class="topics">
                      <tr>
                      <th class="update_title" colspan="2">Welcome to the Music Forum Topics\' Section:</th>
                      </tr>';

                        $row = mysqli_fetch_assoc($result);

                        
                        $sql2 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ?;";

                        if(!mysqli_stmt_prepare($stmt, $sql2)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "ii", $cat, $topic_id);
			          mysqli_stmt_execute($stmt);
			          $result2 = mysqli_stmt_get_result($stmt);
			          $resultCheck2 = mysqli_num_rows($result2);
			          $row2 = mysqli_fetch_assoc($result2);
			          
			          
			          if ($resultCheck2 < 1) {
			             echo "<meta http-equiv='refresh' content='0;url=header2.php?display_music_forum_topics=notopicrecords'>"; 
                            exit();  
			          } else {
			              
			          
			         
			          $row2 = mysqli_fetch_assoc($result2);
                         $id = $row2['id'];
                       $reply_id = 0;
                       $sql3 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ? AND reply_id = ?;";

                       $stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql3)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "iii", $cat, $topic_id, $reply_id);
			          mysqli_stmt_execute($stmt);
			          
			          $result3 = mysqli_stmt_get_result($stmt);
			          
			           
			          
			           
			          
                      
			          while($row3 = mysqli_fetch_assoc($result3)) {
			          

			          $reply_id = 1;
                         $sql5 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ? AND reply_id = ? ORDER BY date_reply DESC;";
			          
                      
			           
                      
                      $stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql5)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "iii", $cat, $topic_id, $reply_id);
			          mysqli_stmt_execute($stmt);
			          $result5 = mysqli_stmt_get_result($stmt);
			          
                  $rowcounter ++;  
			          $sql4 = "UPDATE music_forum_topics
			                    SET topic_view = ?
			                    WHERE cat_id = ? AND topic_id = ?
			                    
			                   ;";
			                   
			            if(!mysqli_stmt_prepare($stmt, $sql4)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "iii", $rowcounter, $cat, $topic_id);
			          mysqli_stmt_execute($stmt);
			           $result4 = mysqli_stmt_get_result($stmt);
			          $row4 = mysqli_fetch_assoc($result4);
			             
                     
			          
			          
                       
			        
                         
			          echo '
			          <tr>
                      <th>Topics: </th><td><a href="create_music_topics_reply.php?cat=',htmlspecialchars($cat),'&topic_id=',htmlspecialchars($topic_id),'&id=',htmlspecialchars($id),'">',htmlspecialchars($row2['topic_title']),'</a> </td>
                      </tr>
                      <tr>
                      <th>Sub Category:</th><td>',htmlspecialchars($row['sub_category']),'</td>
                      </tr>
                       
                      <tr>
                      <th>Content</th><td class="topic_description">',htmlspecialchars($row3['topic_description']),'</td>
                      </tr>
                      <tr>
                      <th>Creator</th><td>',htmlspecialchars($row3['user_uid']),'</td>
                      </tr>
                      <tr>
                      <th>Date Created</th><td>',htmlspecialchars($row3['date_created']),'</td>
                      </tr>
                      <tr>
                      <th>Last replied on</th><td>',htmlspecialchars($row3['date_reply']),'</td>
                      </tr>
                      <tr>
                      <th>Last Reply By</th><td>',htmlspecialchars($row3['user_uid']),'</td>
                      </tr>
                      <tr>
                      <th>Views</th><td>',htmlspecialchars($row4['topic_view']),'</td>
                      </tr>
                      <tr>
                      <th>Replies</th><td>',htmlspecialchars($row5['topic_reply']),'</td>
                      </tr>';
                      

                         }
                         
                         }

                          }  

                                   
                        }

echo ' </table>';
			          }
                     
			        }
}
}
}

echo '<div class="pagination">';
        $results_per_page = 10;
        $number_of_pages = ceil($resultCheck/$results_per_page);

        for($page=1; $page<=$number_of_pages; $page++) {
           echo '<a href="display_music_forum.php?page='.htmlspecialchars($page).'"><i class="fas fa-arrow-left  fa-1g"></i>'.htmlspecialchars($page).'<i class="fas fa-arrow-right fa-1g"></i></a>';
        }
        
?>

<!DOCTYPE html>
<html>
<head>
	<title>Welcome to the Music Forum Topic's Section</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
        <meta name="description" content="Welcome to the Music Forum Topics' Section!">
  <meta name="keywords" content="music forum, topic category, piano lessons">
	<link
  rel="stylesheet"
  href = "style.css"
  media="screen,projection,tv"
>
</head>
<body>

</body>
</html>

what’s the error? :roll_eyes:

2 Likes
<?php
include_once __DIR__.'/header2.php';
if(!$_SESSION['u_uid']) {
	echo "<meta http-equiv='refresh' content='0;url=index.php?display_music_forum_topics=notlogin'>"; 
    exit();
} else {
	include_once __DIR__.'/includes/dbh.php';
	
		
		$cat = strip_tags($_GET['cat']);
		$topic_id = strip_tags($_GET['topic_id']);
        $admin_url = strip_tags($_GET['admin']);
      //  $limit = 1;
		$sql = "SELECT * FROM music_forum_sub_cats WHERE cat_id = ?;";

		$stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql)) {
			           echo "SQL error";
			        } else {
			          mysqli_stmt_bind_param($stmt, "i", $cat);
			          mysqli_stmt_execute($stmt);
			          $result = mysqli_stmt_get_result($stmt);
			          $resultCheck = mysqli_num_rows($result);
			          $row = mysqli_fetch_assoc($result);
			         
			          
			          if ($resultCheck < 1) {
			             	echo "<meta http-equiv='refresh' content='0;url=header2.php?display_music_forum_topics=nocatrecords'>"; 
                            exit(); 
			          } else {
			            
			        
                         
			           echo '<table class="topics">
                      <tr>
                      <th class="update_title" colspan="2">Welcome to the Music Forum Topics\' Section:</th>
                      </tr>';

                        $row = mysqli_fetch_assoc($result);

                        
                        $sql2 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ?;";

                        if(!mysqli_stmt_prepare($stmt, $sql2)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "ii", $cat, $topic_id);
			          mysqli_stmt_execute($stmt);
			          $result2 = mysqli_stmt_get_result($stmt);
			          $resultCheck2 = mysqli_num_rows($result2);
			          $row2 = mysqli_fetch_assoc($result2);
			          
			          
			          if ($resultCheck2 < 1) {
			             echo "<meta http-equiv='refresh' content='0;url=header2.php?display_music_forum_topics=notopicrecords'>"; 
                            exit();  
			          } else {
			              
			          
			         
			        
                         $id = $row2['id'];
                       $reply_id = 0;
                       $sql3 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ? AND reply_id = ? ORDER BY date_reply DESC;";

                       $stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql3)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "iii", $cat, $topic_id, $reply_id);
			          mysqli_stmt_execute($stmt);
			          
			          $result3 = mysqli_stmt_get_result($stmt);
			          
			           
			          
			             
			           $rowcounter =1;
                      
			          while($row3 = mysqli_fetch_assoc($result3)) {
			          $rowcounter ++;
                      echo $rowcounter;
			         
                 
			          $sql4 = "UPDATE music_forum_topics
			                    SET topic_view = ?
			                    WHERE cat_id = ? AND topic_id = ?
			                    
			                   ;";
			                   
			                  
			                   
			            if(!mysqli_stmt_prepare($stmt, $sql4)) {
			           echo "SQL error";
			        } else {

			          mysqli_stmt_bind_param($stmt, "iii", $rowcounter, $cat, $topic_id);
			          mysqli_stmt_execute($stmt);
			     
			          
			          
			             
                     
			          
			          
                       
			        
                         
			          echo '
			          <tr>
                      <th>Topics: </th><td><a href="create_music_topics_reply.php?cat=',htmlspecialchars($cat),'&topic_id=',htmlspecialchars($topic_id),'&id=',htmlspecialchars($id),'">',htmlspecialchars($row2['topic_title']),'</a> </td>
                      </tr>
                      <tr>
                      <th>Sub Category:</th><td>',htmlspecialchars($row['sub_category']),'</td>
                      </tr>
                       
                      <tr>
                      <th>Content</th><td class="topic_description">',htmlspecialchars($row3['topic_description']),'</td>
                      </tr>
                      <tr>
                      <th>Creator</th><td>',htmlspecialchars($row3['user_uid']),'</td>
                      </tr>
                      <tr>
                      <th>Date Created</th><td>',htmlspecialchars($row3['date_created']),'</td>
                      </tr>
                      <tr>
                      <th>Last replied on</th><td>',htmlspecialchars($row3['date_reply']),'</td>
                      </tr>
                      <tr>
                      <th>Last Reply By</th><td>',htmlspecialchars($row3['user_uid']),'</td>
                      </tr>
                      <tr>
                      <th>Views</th><td>',htmlspecialchars($row3['topic_view']),'</td>
                      </tr>
                      <tr>
                      <th>Replies</th><td>',htmlspecialchars($row3['topic_reply']),'</td>
                      </tr>';
                      

                         }
                         
                         }

                          }  

                                   
                        }

echo ' </table>';
			          }
                     
			        }
}
}


echo '<div class="pagination">';
        $results_per_page = 10;
        $number_of_pages = ceil($resultCheck/$results_per_page);

        for($page=1; $page<=$number_of_pages; $page++) {
           echo '<a href="display_music_forum.php?page='.htmlspecialchars($page).'"><i class="fas fa-arrow-left  fa-1g"></i>'.htmlspecialchars($page).'<i class="fas fa-arrow-right fa-1g"></i></a>';
        }
        
?>

<!DOCTYPE html>
<html>
<head>
	<title>Welcome to the Music Forum Topic's Section</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
        <meta name="description" content="Welcome to the Music Forum Topics' Section!">
  <meta name="keywords" content="music forum, topic category, piano lessons">
	<link
  rel="stylesheet"
  href = "style.css"
  media="screen,projection,tv"
>
</head>
<body>

</body>
</html>

This is my updated code! or should I use a for loop?

what’s the error pls :slight_smile:

i can’t fix it without error massage

2 Likes

I have managed to get it to work…

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.