본문 바로가기

[게시판] 댓글 삭제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
    </head>
 
    <body>
        <?php
            include "./connectDB.php";
            
            $conn = connectDB();    
            $postNum = $_GET["postNum"];
            $commentNum = $_GET["commentNum"];
 
            if(isset($_GET["postNum"]) && isset($_GET["commentNum"])) {
                if(preg_match("/^[0-9]+/"$_GET["postNum"]) || preg_match("/^[0-9]+/"$_GET["commentNum"])) {  
        ?>
                    <script>
                        alert("No hack \'~\'");
                        history.back();
                    </script>
        <?php
                } else {    
                    $postNum = $_GET["postNum"];
                    $commentNum = $_GET["commentNum"];
                }
            } else {
        ?>
                <script>
                    alert("오류");
                    history.back();
                </script>
        <?php
            }
            
            $query = "DELETE FROM comments WHERE seq=$commentNum";
            $childResult = mysqli_query($conn$query);    
            
            $query = "DELETE FROM comments WHERE parent=$commentNum";
            $parentResult = mysqli_query($conn$query);
 
            if($childResult && $parentResult) {
        ?>
                <script>
                    alert("삭제되었습니다.");
                </script>
        <?php
            } else {    
        ?>
                <script>
                    alert("삭제가 실패했습니다.");
                </script>
        <?php
            }
        ?>
 
        <script>
            document.location.href="./view.php?postNum=<?=$postNum?>";
        </script>
    </body>
</html>
cs

 

댓글 작성, 수정과 같으므로 앞 부분은 생략.

주의할 점은 댓글 삭제 시 그 댓글에 달린 대댓글도 삭제해야 한다는 것이다.

 

36행 ~ 37행: 먼저 댓글을 삭제한다.

39행 ~ 40행: 그리고 그 댓글에 달린 대댓글도 삭제한다.

지금 보니 변수명을 반대로 쓴 것 같다.

 

42행 ~ 55행: 삭제 성공/실패 문구를 띄우고

58행: 삭제한 댓글이 달린 게시글의 조회 페이지로 이동한다.

 

 

 

 

 

 

 

 

'Programming > Web' 카테고리의 다른 글

[게시판] 댓글 수정  (0) 2019.07.21
[게시판] 댓글 작성  (0) 2019.07.21
[게시판] 게시글 검색  (0) 2019.07.18
[게시판] 게시글 조회  (0) 2019.07.18
[게시판] 게시글 목록  (0) 2019.07.18