0

I have got the following forum working but just can't get the topic_view to increment each time a user click on a topic. I thought that I could update the topic_view before displaying the topics inside the while loop. I thought that it should work in this way but it does display a boolean error. I thought that my $row4 should be correct but don't know why it is showing a boolean error

    <?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>
piano0011
  • 11
  • 5

1 Answers1

0

I noticed two misuse in this code. You can't use ; in quotation marks for SQL queries.

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

$sql3 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ? AND reply_id = ? ORDER BY date_reply DESC;";

Change two lines to:

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

$sql3 = "SELECT * FROM music_forum_topics WHERE cat_id = ? AND topic_id = ? AND reply_id = ? ORDER BY date_reply DESC";

Can you try your code as like this?

ariferol01
  • 221
  • 4
  • 13