Web Hacking Study/Web Page 만들기

게시판 페이지 개발 (게시글 작성 기능)

silver surfer 2022. 5. 18.

** write.php

게시글을 작성하고 submit → post 방식으로 데이터를 write_ok.php에 전송한다

<!DOCTYPE html>
<html lang="ko">
<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>
    <title>write</title>
</head>
<body>
        <form method="post" action="write_ok.php">
        <table style="padding-top:50px" align=center width=auto border=0 cellpadding=2>
            <tr>
                <td style="height:40; float:center; background-color:rgb(179, 170, 197);">
                    <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="text" name="title" size=60></td>
                        </tr>

                        <tr>
                            <td>내용</td>
                            <td><textarea name="content" cols=50 rows=15></textarea></td>
                        </tr>
                    </table>
                    <center>
                        <input style="height:26px; width:47px; font-size:16px;" type="submit" value="작성">
                    </center>
                </td>
            </tr>
        </table>
    </form>
    </body>
</html>

 

 

** write_ok.php

<?php
    session_start();
    if(!isset($_SESSION['username'])) {
        echo "<script>alert('비회원입니다');";
        echo "window.location.replace('main.php');</script>";
    }

    $conn = mysqli_connect('localhost', 'root', 'mysql', 'board');
    $title = $_POST['title']; //타이틀
    $content = $_POST['content']; //내용
    $name = $_SESSION['name']; //작성자

    //데이터를 board 테이블의 각 컬럼에 추가한다
    $sql = "INSERT INTO board(name, title, content, udate, hit, liked) VALUES ('$name', '$title', '$content', now(), 0, 0);";

    $res = mysqli_query($conn, $sql);

    if($res) {
        echo "<script>alert('게시글이 작성되었습니다.');";
        echo "window.location.replace('list.php');</script>";
    } else {
        echo mysqli_error($conn);
    }
?>

 

 

** 구현

 

① 글쓰기 버튼 클릭

 

 

 

② 게시글 작성 페이지에 게시글 작성 → 완료 버튼 클릭

 

 

게시판 목록에서 글이 업로드 된 걸 볼 수 있다

 

다른 계정의 게시글

댓글