Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Can someone help I want to build a forum and don't know where to start

Can someone help I want to build a forum and don't know where to start

Hey, I have created a few forum scripts. It's not so hard if you understand the core behind it.

For example, PHP and SQL is enough for the backend. You'll need to create some HTML forms to submit information from pages. It's helpful if you know JavaScript so you can use AJAX and other functions to make the site look pretty and run fast!
Can someone help I want to build a forum and don't know where to start

here's some examples...


HTML
HTML:
<form action="new-thread.php" method="post">
    Title<br>
    <input type="text" name="thread_title" placeholder="Title">
    <hr>
    Content<br>
    <textarea name="thread_content" rows="5" style="width:100%;" placeholder="Write something here"></textarea>
    <br>
     <button type="submit">Publish</button>

</form>

general $_POST example:
This example is really just to show you how you can use the $_POST values from your HTML form. htmlspecialchars is just a nice safety feature of PHP to make sure you don't get any malicious text outputs... useful for un sanitized user data, $_GET info, etc.
PHP:
echo "Title: " . htmlspecialchars($_POST['thread_title']) . "<br>";
echo "Content: " . htmlspecialchars($_POST['thread_content']);

new-thread.php example code:
PHP:
<?php
    session_start();
    if(!isset($_SESSION['id']){
     header("Location: login.php");
     exit("You must be logged in to publish a new thread.");
    }
    $created_time = time();
    require_once "config/database.php";
    require_once "classes/User.php";

    // ideally user class/function should have a few things like session verifiers and other logic in place...
    $User = new User(); # my custom class, you'll have to make one or another type of user account session file
    $UserInfo = $User->user_info_from_session($_SESSION['id']); # my custom function, you'll have to make your own

    if($UserInfo == false){
      header("Location: login.php");
      exit("You must be logged in to publish a new thread.");
    }
    $error = false;
    if(!isset($_POST['thread_title']) || strlen(trim($_POST['thread_title'])) == 0){
        $error = "You must provide a title for your content!";
    } else if(!isset($_POST['thread_content']) || strlen(trim($_POST['thread_content'])) == 0){
        $error = "You must say something to publish a new thread!";
    } else {

      /*
        A lot of the code below would go in a class like "Thread" for $Thread = new Thread(); and then like $Thread->create($array_of_thread_info);
      */

    
       $Database = new Database();
       $db_connection = $Database->connect();
       // I will use regular php/sql below instead of using my custom $Database functions so you can see examples:
       $sql = $db_connection->prepare("INSERT INTO forum_threads (thread_title, thread_content, user_id, created_unix_time) VALUES (?,?,?,?)");
       $sql->bind_param("ssii", $_POST['thread_title'], $_POST['thread_content'], $UserInfo['id'], $created_time);
       $sql->execute();
       if($sql->error != ""){
           $error = $sql->error;
       }
      $sql->close();
      
    }

    
    if($error){
        echo "<strong>Error:</strong> $error";
    } else {
        echo "You successfully created a new thread!";
    }
?>

SQL Database:
Code:
table:
forum_threads

-> columns:
--> id (auto increment, int, primary key)
--> thread_title
--> thread_content
--> user_id (int 11)
--> created_unix_time (stores unix time -- or use a timestamp!)

You would also have other tables like:
- user_info
- thread_replies
- forum_sections
- forum_categories
- forum_subcategories
 
Last edited:
I don't suggest you make a forum if you're completely new and a little lost at the idea of doing so.

You need to start with something small, if you start with something big and complex it'll be much, much harder to pick up and learn.

Why not make a simple guest book first?
 

Buy us a coffee!

Back
Top Bottom