I can create a post and it is displayed in a certain area of the website. However, I want to not make all of the posts appear in one area but instead add a feature called communities where you create a community and then you can create posts in that specific community. Below is my code that has a working create a post feature and displaying the post. I have provided some images below to show what my site looks like. Thank you for the help in advance.
Show Post:
<?php
require 'header.php';
require 'includes/db_conn.php';
if (!isset($_SESSION['uid'])) {
header("Location: ./index.php?error=please_log_in");
exit();
}
$postId = $_GET['postId'];
$sql = "UPDATE posts SET views=views+1 WHERE post_id=".$postId.";";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_execute($stmt);
$sql = "SELECT posts.post_id, posts.title, posts.content, posts.video, posts.datePosted, users.username FROM posts INNER JOIN users ON posts.uid=users.uid WHERE ".$postId."=posts.post_id;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../forum.php?error=sqlerror");
exit();
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
echo "<table class='table'>";
echo "<tr><th>Title</th><th>Posted By</th><th>Date Posted</th></tr>";
echo "<tr><td>".$row['title']."</td><td>".$row['username']."</td><td>".$row['datePosted']."</td></tr>";
echo "</table>";
echo "<table class='table'>";
echo "<tr><th>Content</th></tr>";
echo "<tr><td>".$row['content']."</td></tr>";
echo "</table>";
echo "<table class='table'>";
echo "<div class='vidd'>";
$text = preg_replace("#.*youtube\.com/watch\?v=#" , "", $row['video']);
echo "<div class='vidd'>";
$text = '<iframe width="1280" height="800" id="vidd" src="https://www.youtube.com/embed/'.$text.'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
echo "</div>";
echo $text;
echo "</table>";
echo "<form action='includes/replyhandler.php?postId=".$postId."' method='post'>";
echo "<div class='form-group text-center'>";
echo "<label>Post a Comment!</label><br />";
echo "<textarea cols='80' rows='5' id='comment' name='comment'></textarea>";
echo "</div>";
echo "<div class='form-group text-center'>";
echo "<button class='btn btn-primary' type='submit' name='submit-reply'> Add Reply</button>";
echo "</div>";
echo "</form>";
$sql = "SELECT replies.comment, replies.datePosted, users.username FROM replies INNER JOIN users ON replies.uid=users.uid WHERE (".$postId." = replies.post_id) ORDER BY reply_id DESC;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../forum.php?error=sqlerror");
exit();
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
echo "<table class='table'>";
echo "<tr><th>Comment</th><th>Posted By</th><th>Date Posted</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>".$row['comment']."</td><td>".$row['username']."</td><td>".$row['datePosted']."</td></tr>";
}
echo "</table>";
}
} else {
echo "<p> No Content</p>";
}
}
require 'footer.php';
?>
<link rel="stylesheet" href="./video.css">
Create post:
<?php
require 'header.php';
if (!isset($_SESSION['uid'])) {
header("Location: ./index.php?error=please_log_in");
exit();
}
if (isset($_POST['submit-post'])) {
require 'includes/db_conn.php';
$title = $_POST['title'];
$content = $_POST['content'];
$video = $_POST['video'];
$uid = $_SESSION['uid'];
$view = 1;
if (empty($title) || empty($content)) {
header("Location: ../newpost.php?error=empyfield");
exit();
} else {
echo ".$title $content.";
$sql = "INSERT INTO posts (uid, title, content, video, datePosted, views) VALUES (?, ?, ?, ?, now(), ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../newpost.php?error=sqlerror=".mysqli_error($conn)."&uid=".$uid."&title=".$title);
exit();
} else {
mysqli_stmt_bind_param($stmt, "issss", $uid, $title, $content, $video, $view);
mysqli_stmt_execute($stmt);
header("Location: ../forum.php?post=successful");
exit();
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
?>
<link rel="stylesheet" href="./style.css">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class='form-group row'>
<div class="col-md-6 offset-md-3">
<h1 id="createpost">Create a Post</h1>
<label>Title</label><br />
<input type="text" name="title" placeholder="Title"> <br />
<label>Video Link</label>
<br>
<input type='url' id='vid' name='video' placeholder="Video Link"/>
<br>
</form>
<label>Description</label><br />
<textarea cols='97' rows='10' name='content'></textarea>
<button id="btnn" type="submit" name="submit-post">Post</button>
</div>
</div>
</form>
<?php
require 'footer.php';
?>