Skip to content

Commit

Permalink
Added comment deletion functions in comment.php
Browse files Browse the repository at this point in the history
  • Loading branch information
hiimdoublej committed Jun 5, 2016
1 parent 1f8e1d1 commit a58cff1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 23 deletions.
95 changes: 93 additions & 2 deletions comment.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
<?php
date_default_timezone_set("Asia/Taipei");
include_once 'header.php';
if(isset($_POST["delete_msg"]))//deleting message part
{
if($_POST["delete_msg"]=="delete")
{
$sql_select = "SELECT uid FROM messages WHERE msg_id =".$_POST["del_msg_id"];
$target = $conn->query($sql_select);
$target_obj = $target->fetch(PDO::FETCH_OBJ);
if($_SESSION['user']==$target_obj->uid)
{
$sql_del = "DELETE FROM messages WHERE messages.msg_id = ?";
$stmt = $conn->prepare($sql_del);
$stmt->bindValue(1,$_POST["del_msg_id"]);
$stmt->execute();
?>
<script>alert('Message deleted !');</script>
<?php
header("Location: home.php");
}
else
{
echo "Error when deleting message.....";
}
}
}
if(isset($_POST["delete_cmt"]))//deleting comments/replies part
{
if($_POST["delete_cmt"]=="delete")
{
$sql_select = "SELECT uid FROM comments WHERE cmt_id =".$_POST["del_msg_id"];
$target = $conn->query($sql_select);
$target_obj = $target->fetch(PDO::FETCH_OBJ);
if($_SESSION['user']==$target_obj->uid)
{
$sql_del = "DELETE FROM comments WHERE cmt_id = ?";
$stmt = $conn->prepare($sql_del);
$stmt->bindValue(1,$_POST["del_msg_id"]);
$stmt->execute();
?>
<script>alert('Reply deleted !');</script>
<?php
}
else
{
echo "Error when deleting message.....";
}
}
}
echo "<br>";
if(isset($_POST['view_reply_parent']))
{
$_SESSION['view_reply_parent'] = $_POST['view_reply_parent'];
$_SESSION['view_reply_parent'] = $_POST['view_reply_parent'];
}
$parent_id = $_SESSION['view_reply_parent'];
if(isset($_POST['submit_comment']))
Expand Down Expand Up @@ -52,19 +99,41 @@
<tr><th id = "text">Message</th>
<th id = "name">By</th>
<th id = "time">Time</th>
<th id="actions">Delete</th>
<?php
while($row=$res->fetch(PDO::FETCH_OBJ))
{
echo "<tr><td>".$row -> msg."</td>";
echo "<td>".$row -> username."</td>";
echo "<td>".$row -> msg_time."</td>";
if($_SESSION['user']==$row->uid)
{
?>
<td id="actions">
<form action="comment.php" method="POST" >
<button type="submit" id="del_btn" name="delete_msg" value ="delete"
style="border:0;background transparent;"
onclick="return confirm('Delete this message?')"/>
<img style ="width:30px;height:30px;"src="delete.png" class = "invert" title = "Delete This Message" alt="submit" />
<?php
echo "<input type='hidden' name='del_msg_id' value=".$row->msg_id.">"
?>
</form>
</a>
</td>
<?php
}
else
{
echo "<td></td>";
}
echo "</tr>";
}
echo "</table><br>";

//display the replys to that message

$sql_select = "SELECT cmt,cmt_id,username,cmt_time
$sql_select = "SELECT cmt,cmt_id,username,cmt_time,comments.uid
FROM comments
INNER JOIN messages ON $parent_id=comments.parent_id AND parent_id = messages.msg_id
INNER JOIN users ON users.user_id = comments.uid
Expand All @@ -78,12 +147,34 @@
<tr><th id="text">Replies</th>
<th id="name">By</th>
<th id="time">Time</th>
<th id="actions">Delete</th>
<?php
while($row=$res->fetch(PDO::FETCH_OBJ))
{
echo "<tr><td>".$row -> cmt."</td>";
echo "<td>".$row -> username."</td>";
echo "<td>".$row -> cmt_time."</td>";
if($_SESSION['user']==$row->uid)
{
?>
<td id="actions">
<form action="comment.php" method="POST" >
<button type="submit" id="del_btn" name="delete_cmt" value ="delete"
style="border:0;background transparent;"
onclick="return confirm('Delete this reply?')"/>
<img style ="width:30px;height:30px;"src="delete.png" class = "invert" title = "Delete This Message" alt="submit" />
<?php
echo "<input type='hidden' name='del_msg_id' value=".$row->cmt_id.">"
?>
</form>
</a>
</td>
<?php
}
else
{
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
Expand Down
38 changes: 19 additions & 19 deletions home.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@

if(isset($_POST["delete_action"]))//deleting message part
{
if($_POST["delete_action"]=="delete")
{
$sql_select = "SELECT uid FROM messages WHERE msg_id =".$_POST["del_msg_id"];
$target = $conn->query($sql_select);
$target_obj = $target->fetch(PDO::FETCH_OBJ);
if($_SESSION['user']==$target_obj->uid)
{
$sql_del = "DELETE FROM messages WHERE messages.msg_id = ?";
$stmt = $conn->prepare($sql_del);
$stmt->bindValue(1,$_POST["del_msg_id"]);
$stmt->execute();
}
else
if($_POST["delete_action"]=="delete")
{
echo "Error when deleting message.....";
$sql_select = "SELECT uid FROM messages WHERE msg_id =".$_POST["del_msg_id"];
$target = $conn->query($sql_select);
$target_obj = $target->fetch(PDO::FETCH_OBJ);
if($_SESSION['user']==$target_obj->uid)
{
$sql_del = "DELETE FROM messages WHERE messages.msg_id = ?";
$stmt = $conn->prepare($sql_del);
$stmt->bindValue(1,$_POST["del_msg_id"]);
$stmt->execute();
}
else
{
echo "Error when deleting message.....";
}
}
}
}

?>
<div class="table-title">
Expand All @@ -54,11 +54,11 @@
{
?>
<table class = "table-fill">
<tr><th>Message</th>
<th>By</th>
<th>Time</th>
<tr><th id="text">Message</th>
<th id="name">By</th>
<th id="time">Time</th>
<th id="actions">Delete</th>
<th>Replies</th>
<th id="replies">Replies</th>
<?php
while($row=$result->fetch(PDO::FETCH_OBJ))
{
Expand Down
10 changes: 8 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ font-size:25px;
font-weight:bolder;
text-transform:uppercase;
margin-left: 40px;
margin-bottom: 40px;
margin-top: 10px;
}
button#msg_search{
border: 2px solid #fafafa;
Expand Down Expand Up @@ -226,7 +228,7 @@ div.table-title {
border-radius:3px;
border-collapse: collapse;
margin-left:40px;
max-width: 70%;
margin-right:40px;
padding:5px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
animation: float 5s infinite;
Expand Down Expand Up @@ -258,7 +260,11 @@ th#time
}
th#actions
{
max-width:10em;

}
th#replies
{
width:16em;
}

th:first-child {
Expand Down

0 comments on commit a58cff1

Please sign in to comment.