본문 바로가기

[게시판] 댓글 수정

1. 댓글 수정 폼: update_com_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
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html>
<?php
    include "./connectDB.php";
 
    $conn = connectDB();
 
    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
    }
?>
    <head>
        <meta charset="UTF-8" />
        <title> Update comment </title>
    </head>
 
    <body>
        <?php
            $query = "SELECT content FROM comments WHERE seq=$commentNum";
            $result = mysqli_query($conn$query);
            $arr = mysqli_fetch_array($result, MYSQLI_ASSOC);
            $content = $arr["content"];
        ?>    
        
        <div id="update_comment" align="center">    
                <h2> 댓글 수정 </h2>    
        </div>
        <div>
            <form method="POST" action="./update_com.php">
                <table align="center">
                    <tr align="center">
                        <td>
                            <textarea type="text" style="resize:none;" cols="105" rows="15" name="commentCont"><?=$content?></textarea>
                        </td>
                    </tr>
                    <tr align="center">
                        <td>
                            <input type="hidden" name="postNum" value="<?=$postNum?>" />
                            <input type="hidden" name="commentNum" value="<?=$commentNum?>" />
                            <input type="submit" value="작성" />
                        </td>
                    </tr>
            </form>
        </div>        
    </body>            
</html>
cs

 

8행 ~ 28행: GET 값 검증. postNum은 수정한 댓글이 달릴 게시글의 번호를,

 commentNum은 수정될 댓글의 고유번호를 의미한다.

 

35행 ~ 40행: 수정할 댓글의 내용을 읽어와 띄운다.

 

46행 ~ 60행: 수정한 댓글의 내용을 실질적으로 DB의 수정이 이루어지는

 update_com.php 페이지로 전달한다.

 

 

 

 

 

2. 댓글 수정: update_com.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
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
    </head>
 
    <body>
        <?php
            include "./connectDB.php";
            $conn = connectDB();
 
            $postNum = $_GET["postNum"];
            $commentNum = $_GET["commentNum"];
            $content = $_POST["commentCont"];
            $content = htmlspecialchars($content); 
            $query = "UPDATE comments SET content=\"$content\" WHERE seq=$commentNum";
            $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
            }
        ?>
    </body>
</html>
cs

 

 

12행 ~ 15행: update_com_form.php에서 게시글 번호, 댓글 번호, 댓글의 내용을 읽어오고

댓글에는 htmlspecialchars 함수를 사용한다.

 

16행 ~ 17행: DB를 수정한다.

 

19행 ~ 35행: 댓글 수정 성공/실패 문구를 띄우고 수정한 댓글이 달린 게시글의 

 조회 페이지로 이동한다. 

 마찬가지로 document.location.href="./view.php?postNum=<?=$postNum?>";는

 한 번만 써도 됐을 듯.

 

 

 

 

 

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

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