보안을 그리다, 훈이

[Web Hacking / PHP & MySQL] 게시판 구현 - modify.php / modify_action.php 본문

Programming/PHP & MySQL

[Web Hacking / PHP & MySQL] 게시판 구현 - modify.php / modify_action.php

HooNeee 2021. 5. 4. 20:01

이번에는 User Interface가 가져야 할 기능 네가지 CRUD 중 Update 기능을 주제로 구현했다.

 

게시글을 작성한 회원은 게시글 하단의 수정 버튼을 통해 modify.php에 접근하여 수정할 수 있으며, 기타 회원 및 비회원이 URL로 접근시에는 '권한이 없습니다.'라는 alert 창을 띄우고 메인화면인 index.php로 돌아가도록 했다.

 

 

[modify.php 소스코드]

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>
    <style>
        table.table2 {
            border-collapse: separate;
            border-spacing: 1px;
            text-align: left;
            line-height: 1.5;
            border-top: 1px solid #ccc;
            margin: 20px 10px;
        }

        table.table2 tr {
            width: 50px;
            padding: 10px;
            font-weight: bold;
            vertical-align: top;
            border-bottom: 1px solid #ccc;
        }

        table.table2 td {
            width: 100px;
            padding: 10px;
            vertical-align: top;
            border-bottom: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <?php
    $connect = mysqli_connect('127.0.0.1', 'root', 'password', 'db_board') or die("connect failed");
    $number = $_GET['number'];
    $query = "select title, content, date, id from board where number = $number";
    $result = $connect->query($query);
    $rows = mysqli_fetch_assoc($result);

    $title = $rows['title'];
    $content = $rows['content'];
    $userid = $rows['id'];

    session_start();

    $URL = "./index.php";

    if (!isset($_SESSION['userid'])) {
    ?> <script>
            alert("권한이 없습니다.");
            location.replace("<?php echo $URL ?>");
        </script>
    <?php   } else if ($_SESSION['userid'] == $userid) {
    ?>
        <form method="POST" action="modify_action.php">
            <table style="padding-top:50px" align=center width=auto border=0 cellpadding=2>
                <tr>
                    <td style="height:40; float:center; background-color:#3C3C3C">
                        <p style="font-size:25px; text-align:center; color:white; margin-top:15px; margin-bottom:15px"><b>게시글 수정하기</b></p>
                    </td>
                </tr>
                <tr>
                    <td bgcolor=white>
                        <table class="table2">
                            <tr>
                                <td>작성자</td>
                                <td><input type="hidden" name="id" value="<?= $_SESSION['userid'] ?>"><?= $_SESSION['userid'] ?></td>
                            </tr>

                            <tr>
                                <td>제목</td>
                                <td><input type=text name=title size=87 value="<?= $title ?>"></td>
                            </tr>

                            <tr>
                                <td>내용</td>
                                <td><textarea name=content cols=75 rows=15><?= $content ?></textarea></td>
                            </tr>

                        </table>

                        <center>
                            <input type="hidden" name="number" value="<?= $number ?>">
                            <input style="height:26px; width:47px; font-size:16px;" type="submit" value="작성">
                        </center>
                    </td>
                </tr>
            </table>
        </form>
    <?php   } else {
    ?> <script>
            alert("권한이 없습니다.");
            location.replace("<?php echo $URL ?>");
        </script>
    <?php   }
    ?>
</body>

</html>

 

read.php를 제외한 기타 기능과 동일하게 POST 방식을 사용하며, modify_action.php를 통해 DB에 업데이트된다.

 

write.php와 동일한 GUI를 가지고 있으며, DB에 저장되어 있는 글의 제목과 내용을 가져와 text / textarea 폼에 채운다.

 

 

[modify.php 결과]


[modify_action.php 소스코드]

<?php
$connect = mysqli_connect('127.0.0.1', 'root', 'password', 'db_board') or die("connect failed");

$number = $_POST['number'];
$title = $_POST['title'];
$content = $_POST['content'];

$date = date('Y-m-d H:i:s');

$query = "update board set title='$title', content='$content', date='$date' where number=$number";
$result = $connect->query($query);
if ($result) {
?>
    <script>
        alert("수정되었습니다.");
        location.replace("./read.php?number=<?= $number ?>");
    </script>
<?php } else {
    echo "다시 시도해주세요.";
}
?>

 

게시글 번호와 제목, 내용은 modify.php에서 받아오며 날짜는 date 함수를 사용해 수정된 날짜를 update문 쿼리를 통해 DB를 수정한다.

 

수정이 정상적으로 완료되면 '수정되었습니다.'라는 alert 창을 띄운 후 수정된 게시글로 이동한다.

쿼리문이 정상적으로 작동되지 않으면 '다시 시도해주세요.'를 띄운다.

 

 

[modify_action.php 결과]

[수정 결과]

정상적으로 수정된 것을 확인할 수 있다.

 

다음은 삭제 기능을 제공하는 delete.php에 대해 포스팅할 예정이다.

 

Comments