Post and Comment Function in PHP/MySQL

If you are looking for on how to create Post and Comment Function using PHP/MySQL then you are at the right place. This program created in PHP/MySQL to show how to use this post and comment function with timestamp or time ago. It will display the time posted by the user.

Account Information

  • Username: admin
  • Password: admin
  • Username: user
  • Password: user

Database Information

  • Database Name: post_comment
Creating two form field, one for the Registration Form and one for the Log In Form as in the image below. Result - Registration and Log In FormHere's the source code of the image above:
  1. <form id="login_form" method="post" style="float:left;">
  2. <h2>Please Login</h2>
  3. <input type="text" id="username" name="username" placeholder="Enter your User Name" required autofocus="autofocus"><br /><br />
  4. <input type="password" id="password" name="password" placeholder="Enter your Password" required><br /><br />
  5. <button name="login" type="submit">Sign in</button>
  6. </form>
  7. <form method="POST" action="signup.php" id="signup" style="float:right;">
  8. <h2>Register here</h2>
  9. <input type="text" name="username" Placeholder="Enter your User Name"><br /><br />
  10. <input type="password" name="password" Placeholder="Enter your Password"><br /><br />
  11. <input type="text" name="firstname" Placeholder="Enter your First Name"><br /><br />
  12. <input type="text" name="lastname" Placeholder="Enter your Last Name"><br /><br /><br />
  13. <button name="save" type="submit">Save</button>
  14. </form>
Creating post box for the user where they can post something shown in the image below. Result - Post BoxHere's the source code of the image above:
  1. <h2>WELCOME!</h2>
  2. <b>
  3. <?php echo $member_row['firstname']." ".$member_row['lastname']; ?>
  4. </b>
  5. <a href="logout.php"><button>Log Out</button></a>
  6.  
  7. <form method="post">
  8. <textarea name="post_content" rows="7" cols="64" placeholder="Say something . . . . ." required autofocus="autofocus"></textarea>
  9. <br>
  10. <button name="post">&nbsp;POST</button>
  11. <br>
  12. <hr>
  13. </form>
After creating the post box, we are going to create the comment box. The comment box now looks like this. Result - Comment BoxHere's the source code of the image above:
  1. <form method="post" style="background:azure; border:1px solid blue; padding:10px;">
  2. <br>
  3. <input type="hidden" name="id" value="<?php echo $id; ?>">
  4. <textarea name="comment_content" rows="2" cols="44" placeholder="Write a comment . . . . ." required></textarea>
  5. <br>
  6. <input type="submit" class="comment_button" name="comment">
  7. </form>
Here's the source code for the Sign Up Form and Log In Form.

For Sign Up Form

  1. <?php
  2. include ('dbconn.php');
  3.  
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. mysql_query("insert into user (username, password, firstname, lastname) values ('$username', '$password', '$firstname', '$lastname')");
  9. ?>
  10. <script>
  11. alert('Successfully Signed Up! You can now Log in your Account');
  12. window.location = 'index.php';
  13. </script>

For Log In Form

  1. <?php
  2. include ('dbconn.php');
  3.  
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6. $query = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'") or die(mysql_error());
  7. $count = mysql_num_rows($query);
  8. $row = mysql_fetch_array($query);
  9.  
  10. if ($count > 0)
  11. {
  12. $_SESSION['id'] = $row['user_id'];
  13. echo 'true';
  14. }
  15. else
  16. {
  17. echo 'false';
  18. }
  19.  
  20. ?>
The output looks like this. Result - Output Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Comments

Submitted bylaura (not verified)on Sat, 07/02/2022 - 22:25

tahnks

Add new comment