PHP/MySQLi Creating a Forum - Part 1 - Database and Creating Threads

Introduction: This tutorial will be starting my series of creating a forum in PHP/MySQLi/HTML. As this is the first part, we will be; Creating the database. Making a create thread page. 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. Database: We are going to be storing all of the threads and replies in a database named 'forumTutorial', and in there two tables named 'threads' and 'replies'. Let's create these now; Go to PHPMyAdmin and enter 'forumTutorial' followed by pressing the 'create' button. Once it has created, click on the database and add a table named 'threads', put the following information; 4 Columns: id - INT - 5 Length - Primary Key - Auto Increment (AI/A_I). title - VARCHAR - 255 Length. content - VARCHAR - 2000 Length. author - VARCHAR - 255 Length. Next, finish the table and create a new table named 'replies', for this one we want the same layout as our 'threads' table but with an extra column at the end of the table; threadID - INT - 5 Length This extra column will be used to link the replies to the given thread. Now we are ready to work on the create thread form/page... Create Thread Form/Page: So now that we have the database and tables set up and ready to go, we need to give the users an option to populate our tables. First we need threads in order for replies to be given, let's create a form for the user to create a thread...
  1. <html>
  2. <head></head>
  3. <body>
  4. <h1>Create Thread:</h1>
  5. <form action='forumTutorial.php' method='POST'>
  6. <table>
  7. <tbody>
  8. <tr>
  9. <td>Title: </td><td><input type='text' name='title' /></td>
  10. </tr>
  11. <tr>
  12. <td>Description: </td><td><input type='text' name='description' /></td>
  13. </tr>
  14. <tr>
  15. <td></td><td><input type='submit' value='Create Thread' name='createThread' /></td>
  16. </tr>
  17. </tbody>
  18. </table>
  19. </form>
  20. </body>
  21. </html>
We have set the action to forumTutorial.php as we are going to save this form page as forumTutorial.php, this means we only have to create and use one page - it just makes it easier for us and the user. Next we need the PHP;
  1. <?php
  2. //$_SESSION['username'] = 'Admin';
  3. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  4. if (isSet($_POST['createThread'])) {
  5. if (isSet($_POST['title']) && $_POST['title'] != '' && isSet($_POST['description']) && $_POST['description'] != '' && isSet($_SESSION['username']) && $_SESSION['username'] != '') {
  6. $title = $_POST['title'];
  7. $description = $_POST['description'];
  8. $user = $_SESSION['username'];
  9. $q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$description', '$user')") or die(mysql_error());
  10. if ($q) {
  11. echo 'Thread created.';
  12. }else
  13. echo 'Failed to create thread.';
  14. }
  15. }
  16. ?>
We first check if the submit button has been sent (if the form has been used), then we check that there is a valid title from the 'title' field in our form, description from our 'description' and username from our session values of logging in. If there is valid information we insert it in to our threads table within our database with id as nothing (due to it Auto_Incrementing through the database already), title as the form title, content as the form description and author as the username session variable. We finally output the end result. Checking: So that is it, everything should be working, you can check this by submitting the form with custom values, then checking the database to see if the information is inserted correctly. PLEASE NOTE: You require 'session_start();' at the top of each php file in order for your users session to be carried over (the username variable after logging in). Full Source:
  1. <?php
  2. //$_SESSION['username'] = 'Admin';
  3. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
  4. if (isSet($_POST['createThread'])) {
  5. if (isSet($_POST['title']) && $_POST['title'] != '' && isSet($_POST['description']) && $_POST['description'] != '' && isSet($_SESSION['username']) && $_SESSION['username'] != '') {
  6. $title = $_POST['title'];
  7. $description = $_POST['description'];
  8. $user = $_SESSION['username'];
  9. $q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$description', '$user')") or die(mysql_error());
  10. if ($q) {
  11. echo 'Thread created.';
  12. }else
  13. echo 'Failed to create thread.';
  14. }
  15. }
  16. ?>
  17. <html>
  18. <head></head>
  19. <body>
  20. <h1>Create Thread:</h1>
  21. <form action='forumTutorial.php' method='POST'>
  22. <table>
  23. <tbody>
  24. <tr>
  25. <td>Title: </td><td><input type='text' name='title' /></td>
  26. </tr>
  27. <tr>
  28. <td>Description: </td><td><input type='text' name='description' /></td>
  29. </tr>
  30. <tr>
  31. <td></td><td><input type='submit' value='Create Thread' name='createThread' /></td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. </form>
  36. </body>
  37. </html>

Comments

Submitted byprashant singh (not verified)on Fri, 08/08/2014 - 02:09

Hey I Got error of undefined error . I think it is not getting the form field values into some variables

Add new comment