Simple Posting Status Updates with Comment Like Using PHP

Simple Posting Status Updates With Comment Like Using PHP/MYSQL

This is a simple tutorial for creating a Posting Status Updates with Comment Like Facebook Using PHP/MySQL.

But not only for posting, in this tutorial will you also learn on how to create a comment on the post being posted.

This tutorial also contains a very easy to customize cascading style sheet (CSS).

Let’s start our tutorial.

Posting Status Updates with Comment Like Facebook Using PHP/MySQL Steps:

First, create a database then name it as any name you desire. In my case I use “itsourcecode” as my database name.

Then create table then name it as “posts” then put the following attributes.

[sql]

CREATE TABLE `posts` (

`post_id` int(11) NOT NULL,

`post_name` text NOT NULL,

`post_msg` text NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

[/sql]

Create another table then name it as “comments” then put the following attributes.

[sql]

CREATE TABLE `comments` (  `comment_id` int(11) NOT NULL,  `post_id` int(11) NOT NULL,  `user_name` text NOT NULL,  `user_comment` text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

[/sql]

For the CSS, create a “styles.css” file then put the following CSS codes.

[css]

body {

background-color: #5E5E5E;

color: #000000;

width: 70%;

margin: auto;

font: 15px Calibri;

padding-top: 20px;

}

.body {

background-color: #E5E5E5;

padding: 10px;

margin-top: 5px;

}

textarea {

background-color: #ffffff;

padding: 10px;

border-radius: 3px;

border: 1px solid #c0c0c0;

margin: 3px;

margin-bottom: 0px;

}

input[name='uname'] {

background-color: #ffffff;

padding: 10px;

border-radius: 3px;

margin: 3px;

margin-bottom: 4px;

margin-top: 0px;

border: 1px solid #c0c0c0;

width: 154px;

}

.post-panel {

background-color: #FFFFFF;

border: 1px solid #c0c0c0;

margin-bottom: 10px;

border-radius: 5px;

}

.post-header {

padding: 10px;

}

.post-body {

padding: 10px;

border-top: 1px solid #c0c0c0;

}

.post-footer {

padding: 10px;

border-top: 1px solid #c0c0c0;

}

input[type='submit'] {

background-color: #FFFFFF;

border: 1px solid #c0c0c0;

padding: 10px;

border-radius: 3px;

margin: 3px;

margin-top: 0px;

}

a {

color: #0005FF;

text-decoration: none;

}

[/css]

For the connection to the database.  Create a “connection.php” file then put the following codes.

[php]

<?php

$mysqli = new MySQLi('localhost', 'root', '', 'itsourcecode');

?>

[/php]

Create an “index.php” file then put the following codes.

[php]

<?php include ("connection.php");?>

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Comment to Post</title>

<link rel="stylesheet" type="text/css" href="styles.css">

</head>

<body>

<div class="body">

Simple Posting Updates With Comment in PHP and MYSQL

</div>

<div class="body">

<form method="post" action="post-action.php">

<label>Add Post:</label><br>

<textarea name="post_msg" required></textarea><br>

<input type="text" name="uname" placeholder="type your name here..." required /><br>

<input type="submit" name="post" value="Post" />

</form>

<?php

if (isset($_GET['post_action'])) {

if ($_GET['post_action'] == "posted") {

echo 'Successfully Posted!';

}

}

?>

</div>

<?php

$posts = $mysqli->query("SELECT * FROM posts ORDER BY post_id DESC");

?>

<div class="body">

<b><?php echo $posts->num_rows ?></b> total posts

</div>

<div class="body" style="padding-bottom: 1px;">

<?php

if ($posts->num_rows == null) {

echo 'no posts yet';

} else if ($posts->num_rows != null) {

while ($post_data = $posts->fetch_assoc()) { ?>

<div class="post-panel">

<div class="post-header">

<b><?php echo $post_data['post_name'] ?></b>

</div>

<div class="post-body">

<?php echo $post_data['post_msg'] ?>

</div>

<?php

$comments = $mysqli->query("SELECT * FROM comments WHERE post_id = ".$post_data['post_id']."");

?>

<div class="post-footer">

<a href="view-post.php?post_id=<?php echo $post_data['post_id'] ?>">Comment (<?php echo $comments->num_rows ?>)</a>

</div>

</div>

<?php }

}

?>

</div>

</body>

</html>

[/php]

For posting, create a “post-action.php” file then put the following codes.

[php]

<?php

include ("connection.php");

if (isset($_POST['post'])) {

$post_msg = $_POST['post_msg'];

$uname = $_POST['uname'];

$post = $mysqli->query("INSERT INTO posts (post_name, post_msg) VALUES ('$uname', '$post_msg')");

if ($post) {

header("Location: index.php?post_action=posted");

} else {

echo $mysqli->error;

}

}

?>

[/php]

For viewing and commenting on the post, create a “view-post.php” file then put the following codes.

[php]

<?php

include ("connection.php");

if (isset($_GET['post_id'])) {

$id = $_GET['post_id'];

$post = $mysqli->query("SELECT * FROM posts WHERE post_id = $id");

$post_data = $post->fetch_assoc();

} else {

header("Location: index.php");

}

?>

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title><?php echo $post_data['post_name'] ?>'s Status Update</title>

<link rel="stylesheet" type="text/css" href="styles.css">

</head>

<body>

<div class="body">

<a href="index.php">Home</a> | <b><?php echo $post_data['post_name'] ?>'</b>s Status Update

</div>

<div class="body">

<div class="post-panel">

<div class="post-body" style="border: none;">

<?php echo $post_data['post_msg'] ?>

</div>

<div class="post-footer">

<?php

$comments = $mysqli->query("SELECT * FROM comments WHERE post_id = $id");

?>

<b><?php echo $comments->num_rows ?></b> total comments<br><br>

<?php

while ($comment_data = $comments->fetch_assoc()) { ?>

<div class="post-panel">

<div class="post-header">

<b><?php echo $comment_data['user_name'] ?></b>

</div>

<div class="post-body">

<?php echo $comment_data['user_comment'] ?>

</div>

</div>

<?php }

?>

<form method="post" action="comment-action.php?post_id=<?php echo $id ?>">

<label>Quick Comment:</label><br>

<textarea name="comment" required></textarea><br>

<input type="text" name="uname" placeholder="enter your name here..." required /><br>

<input type="submit" name="post_comment" />

</form>

</div>

</div>

</div>

</body>

</html>

[/php]

For posting a comment on the post, create a “comment-action.php” file then put the following codes.

[php]

<?php

include 'connection.php';

if (isset($_POST['post_comment'])) {

$post_id = $_GET['post_id'];

$comment = $_POST['comment'];

$uname = $_POST['uname'];

$comment = $mysqli->query("INSERT INTO comments (post_id, user_name, user_comment) VALUES ($post_id, '$uname', '$comment')");

if ($comment) {

header("Location: view-post.php?post_id=$post_id");

} else {

echo $mysqli->error;

}

}

?>

[/php]

If you have any questions or suggestion about Posting Status Updates with Comment Like Facebook Using PHP/MySQL, please feel free to contact me at our contact page.

Frequently Asked Questions

How does this PHP Point of Sale system work?

Cashier UI for adding items to cart, scanning barcodes, applying discounts, accepting cash/card, printing receipts (window.print or TCPDF). Real-time inventory deduction. Daily Z-report.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Related PHP Projects

3 thoughts on “Simple Posting Status Updates with Comment Like Using PHP”

Leave a Comment