본문으로 바로가기

게시판의 글쓰기 기능에 대한 포스팅입니다. 


우선 글 작성 폼은 


이렇게 간단하게 했습니다. css나 js를 잘만지는 편도아닐 뿐더러 디자인 감각도 떨어져서.. 그냥 심플하게 만들었습니다. 



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE>
 
<html>
<head>
        <meta charset = 'utf-8'>
</head>
<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>
<body>
        <form method = "get" action = "write_action.php">
        <table  style="padding-top:50px" align = center width=700 border=0 cellpadding=2 >
                <tr>
                <td height=20 aligncenter bgcolor=#ccc><font color=white> 글쓰기</font></td>
                </tr>
                <tr>
                <td bgcolor=white>
                <table class = "table2">
                        <tr>
                        <td>작성자</td>
                        <td><input type = text name = name size=20> </td>
                        </tr>
 
                        <tr>
                        <td>제목</td>
                        <td><input type = text name = title size=60></td>
                        </tr>
 
                        <tr>
                        <td>내용</td>
                        <td><textarea name = content cols=85 rows=15></textarea></td>
                        </tr>
 
                        <tr>
                        <td>비밀번호</td>
                        <td><input type = password name = pw size=10 maxlength=10></td>
                        </tr>
                        </table>
 
                        <center>
                        <input type = "submit" value="작성">
                        </center>
                </td>
                </tr>
        </table>
        </form>
</body>
</html>
 
 
cs



form 태그로 작성한 내용들을 write_action.php로 넘겨서 처리합니다.  write_action.php에서는 받은 데이터를 DB에 저장합니다. 이 게시판은 그냥 기능만 되도록해서 모든 form method 을 GET으로 해놨기 때문에 POST로 바꾸시는걸 권장합니다 ~-~





write_action.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
 <?php
                $connect = mysqli_connect("localhost""""""") or die("fail");
                
                $id = $_GET[name];                      //Writer
                $pw = $_GET[pw];                        //Password
                $title = $_GET[title];                  //Title
                $content = $_GET[content];              //Content
                $date = date('Y-m-d H:i:s');            //Date
 
                $URL = './index.php';                   //return URL
 
 
                $query = "insert into board (number,title, content, date, hit, id, password) 
                        values(null,'$title', '$content', '$date',0, '$id', '$pw')";
 
 
                $result = $connect->query($query);
                if($result){
?>                  <script>
                        alert("<?php echo "글이 등록되었습니다."?>");
                        location.replace("<?php echo $URL?>");
                    </script>
<?php
                }
                else{
                        echo "FAIL";
                }
 
                mysqli_close($connect);
?>
 
cs



모두 GET방식으로 전달하였고, DB에 number, title, content, date, hit, id, password를 insert합니다. 숫자는 자동으로 올라가기 때문에 null 값을 삽입하면 됩니다. 


데이터가 정상적으로 저장되었으면 alert창을 띄우고 목차 페이지로 되돌아 갑니다. 


실패 했을경우 , FAIL을 출력합니다. 





내용을 입력하고 db를 확인하면 잘 저장되어 있는 것을 볼 수 있습니다. 





목차 페이지에도 출력이 잘 되는 것을 볼 수 있습니다 ~


다음 포스팅은 작성된 글을 보는 view 기능입니다