본문 바로가기

[게시판] 댓글 작성

1. 댓글 작성 폼: comment_form.php

 

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
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
    </head>
 
    <?php
        if(isset($_GET["postNum"]) && isset($_GET["prnt"])) {
            $postNum = $_GET["postNum"];
            $parent = $_GET["prnt"];
        } else {
    ?>    
            <script>
                alert("오류");
                history.back();
            </script>
    <?php
        }
 
        if(preg_match("/[^0-9]+/"$_GET["postNum"]) || preg_match("/[^0-9]+/"$_GET["prnt"])) {
    ?>    
            <script>
                alert("No hack \'~\'");
                history.back();
            </script>
    <?php
        }
    ?>
    <body>
        <div id = main align="center">    
            <h2> 댓글 작성 </h2>
        </div>
            <form method="post" action="./comment.php?postNum=<?=\$postNum?>&prnt=<?=\$parent?>">
                <table align="center">        
                    <tr align="center">            
                        <td
                            <textarea type="text" style="resize:none;" cols="105" rows="15" name="cont"></textarea>
                        <td>
                    </tr>
 
                    <tr align="center">
                        <td
                            <input type="submit" value="작성"/>
                        </td>
                    <tr>
                <table>        
            </form>
        </div>
    </body>    
</html>
cs

 

33행: 작성된 댓글을 실질적으로 DB에 저장하는 페이지인 comment.php에 값을 전달한다.

  이 때 댓글이 달릴 게시글의 번호는 GET 방식으로, 댓글의 내용은 POST 방식으로 

  전달된다.

 

 

 

2. 댓글 작성: comment.php

 

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
62
63
<?php
    include "./connectDB.php";
    include "./session.php";
 
    $conn = connectDB();
    $content = $_POST["cont"]; 
 
    if($content === "") {
?>
        <script>
            alert("내용을 입력하세요.");
            history.back();
        </script>
<?php
    } else {
        $content = htmlspecialchars($content);
    }
 
    if(isset($_GET["postNum"]) && isset($_GET["prnt"])) {    
        if(preg_match"/[^0-9]+/"$_GET["idx"])) {
?> 
            <script>
                alert("No hack \'~\'");
                history.back();
            </script>
<?php
           } else {  
            $postNum = $_GET["postNum"];
            $parent = $_GET["prnt"];
        }
    } else {
?>
        <script>
            alert("오류");
            history.back();
        </script>
<?php
    }    
 
    $query = "SELECT name FROM users WHERE id=\"$id\"";
    $result = mysqli_query($conn$query);
    $arr = mysqli_fetch_array($result);
    $writer = $arr["name"];
    
    $query = "INSERT INTO comments(postSeq, writer, content, commented, parent) VALUES ($postNum, \"$writer\", \"$content\", now(), $parent)";
    $result = mysqli_query($conn$query);
    
    if($result) {
?>        
        <script>
            alert("댓글 작성 완료");
            document.location.href="./view.php?postNum=<?=$postNum?>";
        </script>
<?php
    } else {
?>
        <script>
            alert("댓글 작성 실패");
            document.location.href="./view.php?postNum=<?=$postNum?>";
        </script>
<?php
    }
?>
cs

 

 

8행: 작성된 댓글의 내용이 비어 있으면

12행: 이전 페이지로 이동하고,

16행: 그렇지 않으면 htmlspecialchars 함수를 이용해 특수 문자를 html entity로 변환한다.

 

19행 ~ 38행: GET 값(댓글이 달릴 게시글 번호, 상위 댓글의 고유 번호)을 검증한다.

 

40행 ~ 43행: 현재 로그인 되어 있는 사용자의 이름을 얻어서

45행 ~ 46행: DB에 새로운 댓글을 저장한다.

 

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

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