Forum Tutorial - Deleting Posts

Introduction: This tutorial is on how to add the ability for a user to delete their posts. The Theory: We could add a user panel where it lists all the users posts along with options to delete them, but instead we are going to add a delete option adjacent to the post itself to save space - plus I'm planning on making an admin panel tutorial soon. The PHP: So in the part of the PHP where we actually output each reply to a thread, we want to check if the logged in users' username held in the session variable is equal to the author of the post, if it is then we add an option to delete the reply...
  1. $replies .= '<tr><td>'.$row["content"].'</td><td>'.$author.'</td><td>'.$repliedUser["signature"].'</td>';
  2. if (isSet($_SESSION['username']) && $author == $_SESSION['username'])
  3.         $replies .= '<td><a href="threadPage.php?act=delete&type=reply&id='.$row["id"].'">Delete</a></td>';
  4. $replies .= '</tr>';
We do a similar thing for the threads but in a slightly different place to make the layout more clear...
  1. $qq = mysqli_query($con, "SELECT * FROM `threads` WHERE `id`='$id'");
  2. if (isSet($_SESSION['username']) && strtolower($_SESSION['username']) == strtolower($author))
  3.         echo '<a href="threadPage.php?act=delete&type=thread&id='.$id.'">Delete Thread</a>';
  4. echo "
  5. <h3>";
  6.         if (mysqli_num_rows($qq) > 0) {
  7.                 $info = mysqli_fetch_array($qq);
  8.                 $all = $info['rating'];
  9.                 $total = $info['totalRatings'];
  10.                 if ($all == 0 || $all == null || $total == 0 || $total == null) {
  11.                         echo 'No ratings have yet been given for this thread.';
  12.                 }else {
  13.                         $average = $all / $total;
  14.                         echo 'Average Rating: '.$average;
  15.                 }
  16.         }
  17. echo "</h3>
Deleting The Posts: Finally we need the actual PHP script to delete the given post. We add a link around each delete option to the current page with "?act=delete&type=" followed by the type of post (thread or reply) then "&id=" followed by the post ID so that when the delete button is clicked it links to the given page with the correct parameters through PHP GET statements. So now we need to check if the act parameter is set to delete, and if it is, we delete the post from the table with the appropriate ID from the appropriate table...
  1. if (isSet($_GET['act']) && $_GET['act'] == 'delete') {
  2.         if (isSet($_GET['type']) && isSet($_GET['id'])) {
  3.                 $delq;
  4.                 $delAuthor;
  5.                 $delID = $_GET['id'];
  6.                 $delType = $_GET['type'];
  7.                 if ($delType == 'reply') {
  8.                         $delAuthorQ = mysqli_query($con, "SELECT * FROM `replies` WHERE `id`='$delID'");
  9.                         $delAuthorInfo = mysqli_fetch_array($delAuthorQ);
  10.                         $delAuthor = $delAuthorInfo['author'];
  11.                         if (strtolower($delAuthor) == strtolower($_SESSION['username'])) {
  12.                                 $delq = mysqli_query($con, "DELETE FROM `replies` WHERE `id`='$delID'");
  13.                         }else
  14.                                 echo 'You do not have permission to do that!';
  15.                 }else if ($delType == 'thread') {
  16.                         $delAuthorQ = mysqli_query($con, "SELECT * FROM `threads` WHERE `id`='$delID'");
  17.                         $delAuthorInfo = mysqli_fetch_array($delAuthorQ);
  18.                         $delAuthor = $delAuthorInfo['author'];
  19.                         if (strtolower($delAuthor) == strtolower($_SESSION['username'])) {
  20.                                 $delq = mysqli_query($con, "DELETE FROM `threads` WHERE `id`='$delID'");
  21.                         }else
  22.                                 echo 'You do not have permission to do that!';
  23.                 }
  24.                 if ($delq) {
  25.                         echo 'Deleted post successfully.';
  26.                         if ($delType == 'thread') {
  27.                                 header("Location:forumTutorial.php");
  28.                                 exit();
  29.                         }
  30.                 }else
  31.                         echo 'Failed to delete post.';
  32.         }
  33. }

Add new comment