Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!
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
<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>
echo "Title: " . htmlspecialchars($_POST['thread_title']) . "<br>";
echo "Content: " . htmlspecialchars($_POST['thread_content']);
<?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!";
}
?>
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!)
Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.