본문 바로가기

[게시판] 게시글 작성

1. 게시글 작성 폼: write_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
<!DOCTYPE html>
<?php
    include "./connectDB.php";
    include "./session.php";
 
    $conn = connectDB();
    $query = "SELECT * FROM user WHERE id=\"$id\"";
    $result = mysqli_query($conn$query);
    $arr = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $writer = $arr["name"];
?>
 
<html>
    <head>
        <meta charset = "UTF-8"/>
        <title> 글 쓰기 </title>
    </head>
 
    <body>
           <form method="post" action="./write.php">
            <input type="hidden" name="writer" value="<?=$writer?>" />
            <table align="center" border="1" width="850px">
                <tr>
                    <td align="center" width="100px"> 제목 </td>
                    <td align="left" width="550px" > <input type="text" name="sub" maxlength="122" style="width: 540px;"/> </td>
                    <td align="center" width="200px"> <?=$writer?></td>
                </tr>
                <tr>
                    <td colspan="3" align="center">
                        <textarea style="width:850px; height:600px; resize:none;" name="cont"></textarea>
                    </td>
                </tr>
            </table>
            </br>
               <div align="center"> 
                <input type="submit" value="작성하기" name="write"/>
            </div>
           </form>
    </body>
</html>
cs

 

3행: DB와의 연결을 위해 connectDB.php를 include한다.

4행: 세션 값 유지 여부(로그인 여부) 검사를 위해 session.php를 include한다.

 

7행: 현재 id를 사용하는 사용자의 모든 컬럼을 읽어오고

9행: 그 정보를 배열의 형태로 반환한 후

10행: 이름을 읽어온다.

 

21행: type 속성에 "hidden"을 주면 name에 준 이름으로 value에 준 값을 보이지 않게 값을 전달할 수 있다.

 

30행: 글을 작성하는 영역으로, 폭은 850px, 높이는 600px, 크기 조절은 불가능하다.

작성 내용은 cont라는 이름으로 전달된다.

2. 게시글 작성: write.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
<?php
    include "./connectDB.php";
    include "./session.php";
 
    $conn = connectDB();
    
    $sub = $_POST["sub"];
    $cont = $_POST["cont"];
    $writer = $_POST["writer"];
 
    if($sub === "" || $cont === "" || $writer === "") {
?>
        <script>
            alert("공백 불가");
            history.back();
        </script>
<?php
        exit();
    } else {
        $sub = htmlspecialchars($sub, ENT_QUOTES);
        $cont = htmlspecialchars($cont, ENT_QUOTES);
 
        $query = "INSERT INTO posts(writer, posted, subject, content, id) VALUES (\"$writer\", now(), \"$sub\", \"$cont\", \"$id\")";
        $postResult = mysqli_query($conn$query);
        
        if($postResult) {
?>            
            <script>
                alert("게시글이 작성되었습니다.");
                document.location.href="./list.php";
            </script>
<?php
        } else {
?>        
            <script>
                alert("게시글 저장에 실패했습니다.");
                document.location.href="./list.php";
            </script> 
<?php
        }    
    }
?>
cs

7행: 작성한 게시글의 제목과

8행: 내용과

9행: 작성자명을 받고

10행: 이 중 하나라도 공백이면 다시 이전 페이지(글쓰기 페이지)로 이동한다.

 

20행: htmlspecialchars 함수는 주어진 문자열에서 다음과 같이 특수한 문자열을 html entity로 바꾼다.

특수 문자 html entity
' &#039;
" &quot;
< &lt;
>  &gt;
& &amp;

html에서 따옴표(', ")는 속성에 주는 값을, 꺾쇠(<, >)는 태그를,  &는 html entity를 나타내는 데에 쓰이기 때문에 이들을 문자로서 표현하려면 위와 같은 변환 작업이 필요하다.

 

23행: now() 함수는 MySQL 내장 함수로, 현재 시간을 datetime 형으로 반환한다.

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

[게시판] 게시글 삭제  (0) 2019.07.18
[게시판] 게시글 수정  (0) 2019.07.17
[게시판] 회원가입  (0) 2019.07.16
[게시판] DB 연결, 로그인 세션 설정 + 로그인  (0) 2019.07.12
[게시판] 데이터베이스 설계  (0) 2019.07.12