Forum Tutorial - User Status and Signatures

Introduction: This tutorial will be on how to add status and signatures to user profiles. The status will show on their profile while their signature will show under their posts. Database Updates: We will need to store the information for both new features, so in our users table within our database we will add two new columns to the end of the structure: status - VARCHAR - 255 Length signature - VARCHAR - 255 Length Editing Registration: Because we add a new row to the users table on registration, we need to edit our query to take in to account the two new columns...
  1. $qq = mysqli_query($con, "INSERT INTO `users` VALUES ('', '$user', '$passMD5', '$email', '0', '', '')");
We leave the two end parameters blank because we want to the default status and signature for a new user account to be empty. Settings: The user will want to change their status and/or signature from time to time so we need to give them a way to do this. On the account/settings page where they are able to change their password, we want to add a new section to change their status, and another new section to change their signature. First we add the HTML...
  1. <h1>Update Status:</h1>
  2. <?php
  3. $q = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$user'");
  4. $info = mysqli_fetch_array($q);
  5. echo '<p>Current Status: '.$info['status'].'</p><br/>';
  6. ?>
  7. <form action='accountPage.php' method='POST'>
  8. <tr>
  9. <td>New Status: </td><td><textarea rows='10' cols='100' name='status'></textarea></td>
  10. </tr>
  11. <tr>
  12. <td></td><td><input type='submit' value='Update Status' name='updateStatus' /></td>
  13. </tr>
  14. </tbody>
  15. </table>
  16. </form>
  17. <h1>Update Signature:</h1>
  18. <?php
  19. $q = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$user'");
  20. $info = mysqli_fetch_array($q);
  21. echo '<p>Current Signature: '.$info['signature'].'</p><br/>';
  22. ?>
  23. <form action='accountPage.php' method='POST'>
  24. <tr>
  25. <td>New Signature: </td><td><textarea rows='10' cols='100' name='sig'></textarea></td>
  26. </tr>
  27. <tr>
  28. <td></td><td><input type='submit' value='Update Signature' name='updateSignature' /></td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </form>
Then we add the PHP code to process the HTML forms...
  1. if (isSet($_POST['updateStatus']) && isSet($_POST['status'])) {
  2. $status = $_POST['status'];
  3. $q = mysqli_query($con, "UPDATE `users` SET `status`='$status' WHERE `username`='$user'");
  4. if ($q) {
  5. echo 'Update completed successfully.';
  6. }else
  7. echo 'Failed to update user status.';
  8. }
  9. if (isSet($_POST['updateSignature']) && isSet($_POST['sig'])) {
  10. $sig = $_POST['sig'];
  11. $q = mysqli_query($con, "UPDATE `users` SET `signature`='$sig' WHERE `username`='$user'");
  12. if ($q) {
  13. echo 'Update completed successfully.';
  14. }else
  15. echo 'Failed to update user signature.';
  16. }
Posts (Signature): Next we are going to display the users signature under their posts (threads and replies). First we need to grab the users signature from the database table, then we simply output it through HTML... -From-:
  1. $qu = mysqli_query($con, "SELECT * FROM `replies` WHERE `threadID`='$id'");
  2. if (mysqli_num_rows($qu) > 0) {
  3. $replies = '<table><tbody>';
  4. while ($row = mysqli_fetch_array($qu)) {
  5. $replies .= '<tr><td>'.$row["content"].'</td><td>'.$row["author"].'</td></tr>';
  6. }
  7. $replies .= '</tr></tbody></table>';
  8. }
-To-:
  1. $qu = mysqli_query($con, "SELECT * FROM `replies` WHERE `threadID`='$id'");
  2. if (mysqli_num_rows($qu) > 0) {
  3. $replies = '<table><tbody>';
  4. while ($row = mysqli_fetch_array($qu)) {
  5. $author = $row["author"];
  6. $qr = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$author'");
  7. $repliedUser = mysqli_fetch_array($qr);
  8. $replies .= '<tr><td>'.$row["content"].'</td><td>'.$author.'</td><td>'.$repliedUser["signature"].'</td></tr>';
  9. }
  10. $replies .= '</tr></tbody></table>';
  11. }
I have just appended it on to the end of the reply line, you may want to add it underneath but since I am not using CSS in these tutorials I have kept it simple.

Add new comment