PHP/MySQLi Creating a Forum - Part 4 - Adding & Listing Replies

Submitted by: 
Language: 
Visitors have accessed this post 1291 times.

PHP/MySQLi Creating a Forum - Part 3 - Thread Page

Introduction:
This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. This is the fourth and (for now) the final part which is adding reply functionality.

Pre-creation:
First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities.

Also, this will not be covering creating users, or styling the pages. For the purpose of using the logged in users username, we will be using $_SESSION['username']; from my login script, you can find that tutorial on my profile page.

Obviously you will also need to go through the first, second and third parts of this tutorial series which can both be found on my profile tracking page.

Thread Page Editing:
Last tutorial we created a separate thread page in order for us to offer the chance of adding replies. So, we are going to add a form to add a reply at the bottom of the page;

  1. <br/>
  2. <h1>Leave a Reply:</h1>
  3. <form action=<?php echo 'threadPage.php?tid='.$_GET['tid'];?> method='POST'>
  4. <table>
  5. <tbody>
  6. <tr>
  7. <td>Name: </td><td><?php echo $_SESSION['username']; ?></td>
  8. </tr>
  9. <tr>
  10. <td>Message: </td><td><input type='text' name='cont' /></td>
  11. </tr>
  12. <tr>
  13. <td></td><td><input type='submit' value='Post Reply' name='replySent' /></td>
  14. </tr>
  15. </tbody>
  16. </table>
  17. </form>

As you can (and should be able to) see, we are setting the action of the form to the same page (threadPage.php) along with the thread ID of the current tid, this keeps the content the same whilst and after adding the reply.

Table:
I have decided to do the table a little bit different to what I first said in the first part of this tutorial series. We are going to have the following structure:
id - int - 5 Length - AI - Primary Key
threadID - VARCHAR - 255 Length
content - VARCHAR - 255 Length
author - VARCHAR - 255 Length

And put the table name to 'replies' (you can delete the other one if you made one from the first tutorial, sorry about that!)

PHP Programming:
Next, we need to process the reply form using some PHP. Again, we are going to check and confirm the validity of the passing variables, we are then going to insert the reply as a new row if each variable is ok. Finally we give the output;

  1. if (isSet($_POST['replySent']) && isSet($_POST['cont']) && $_POST['cont'] != '' && isSet($_GET['tid']) && $_GET['tid'] != '') {
  2. $cont = $_POST['cont'];
  3. $thread = $_GET['tid'];
  4. $user = $_SESSION['username'];
  5. $q = mysqli_query($con, "INSERT INTO `replies` VALUES ('', '$thread', '$cont', '$user')");
  6. if ($q) {
  7. echo 'Reply submitted.';
  8. }else
  9. echo 'Failed inserting reply.';
  10. }

Listing Replies:
Finally, we need to list all the replies relevant to each thread. On the threadPage we are going to get the thread id (tid) from the get url of the page, we are then going to run a query to get all the rows within the replies table which have threadID as the tid, and output each one separately;

  1. $qu = mysqli_query($con, "SELECT * FROM `replies` WHERE `threadID`='$id'");
  2. if (mysqli_num_rows($qu) > 0) {
  3. $replies = '<table><tbody>';
  4. while ($row = mysqli_fetch_array($qu)) {
  5. $replies .= '<tr><td>'.$row["content"].'</td><td>'.$row["author"].'</td></tr>';
  6. }
  7. $replies .= '</tbody></table>';
  8. }

We want to put the above code just underneath where we set $info, this is because that means we have validated our tid and it is corrent. We are putting all of the replies in to one variable (named replies) as a table of HTML so we can simply output it later by writing the variable. Because of this, we will get an error if there are no replies because $replies won't exist, to fix this just create $replies as a blank variable outside of any if statements;

  1. $replies = '';

Lastly we want to output the variable $replies through PHP in to our HTML body at the bottom of our thread page;

  1. <?php echo $replies; ?>

Finished!
Congrats on finishing the tutorial series, feel free to send me a message for any advice, notes, queries or feedback you have, thank you!

Full Source:
Here is the full source for the threadPage.php;

  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  3. if (isSet($_POST['replySent']) && isSet($_POST['cont']) && $_POST['cont'] != '' && isSet($_GET['tid']) && $_GET['tid'] != '') {
  4. $cont = $_POST['cont'];
  5. $thread = $_GET['tid'];
  6. $user = $_SESSION['username'];
  7. $q = mysqli_query($con, "INSERT INTO `replies` VALUES ('', '$thread', '$cont', '$user')");
  8. if ($q) {
  9. echo 'Reply submitted.';
  10. }else
  11. echo 'Failed inserting reply.';
  12. }
  13. $replies = '';
  14. if (isSet($_GET['tid']) && $_GET['tid'] != '') {
  15. $id = $_GET['tid'];
  16. $query = mysqli_query($con, "SELECT * FROM `threads` WHERE `id`='$id'");
  17. if (mysqli_num_rows($query) > 0) {
  18. $info = mysqli_fetch_array($query);
  19. $qu = mysqli_query($con, "SELECT * FROM `replies` WHERE `threadID`='$id'");
  20. if (mysqli_num_rows($qu) > 0) {
  21. $replies = '<table><tbody>';
  22. while ($row = mysqli_fetch_array($qu)) {
  23. $replies .= '<tr><td>'.$row["content"].'</td><td>'.$row["author"].'</td></tr>';
  24. }
  25. $replies .= '</tbody></table>';
  26. }
  27. $title = $info['title'];
  28. $content = $info['content'];
  29. $author = $info['author'];
  30. }else
  31. echo 'fake tid.';
  32. }else
  33. echo 'fail tid.';
  34. ?>
  35. <html>
  36. <head></head>
  37. <body>
  38. <h1><?php echo $title; ?></h1>
  39. <p><?php echo $content; ?></p>
  40. <br/>
  41. <p>By <?php echo $author; ?></p>
  42. <br/>
  43. <h1>Leave a Reply:</h1>
  44. <form action=<?php echo 'threadPage.php?tid='.$_GET['tid'];?> method='POST'>
  45. <table>
  46. <tbody>
  47. <tr>
  48. <td>Name: </td><td><?php echo $_SESSION['username']; ?></td>
  49. </tr>
  50. <tr>
  51. <td>Message: </td><td><input type='text' name='cont' /></td>
  52. </tr>
  53. <tr>
  54. <td></td><td><input type='submit' value='Post Reply' name='replySent' /></td>
  55. </tr>
  56. </tbody>
  57. </table>
  58. </form>
  59. <br/>
  60. <?php echo $replies; ?>
  61. </body>
  62. </html>

Comments

it won't show me when someone are replying to the thread, ive done what you write here, and i tried myself too, but it wont work...

for project comment page

ppgndt

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.