php) 게시판 만들기 (글작성, 글목록, 글읽기)

writing.php

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>BBS demo</title>
</head>
<body>
    <form method="post" action="writing_action.php">
        <h1>writing</h1>
        <h3>title<input type="text" name="title"></h3>
        <h3>writer<input type="text" name="writer"></h3>
        <h3>content</h3><br>
        <textarea rows="7" cols="33" name="content"></textarea></br>
        <button type="submit">submit</button>
        <button type="reset">reset</button>
    </form>
</body>
</html>

writing_action.php

<?php
$title=$_POST['title'];
$writer=$_POST['writer'];
$content=$_POST['content'];
// mysql 연결
$host = "localhost";
$user = "root";
$pw = "52273178";
$dbName = "study_db";
$conn = new mysqli($host, $user, $pw, $dbName);
//INSERT
# $sql = "INSERT INTO BBS VALUES(NULL ,$title,$content,$writer,NOW(),1)";
$sql = "INSERT INTO BBS(title,writer,content,publish) VALUES('$title','$writer','$content',NOW())";
if ($conn->query($sql) === TRUE) { ?>
<script>
    alert("<?php echo "alert: 게시글이 등록되었습니다." ?>");
    location.replace("<?php echo './list.php' ?>");
</script>
<?php
} else { ?>
    <script>
        alert("<?php echo "Error: " . $sql . "<br>" . $conn->error; ?>")
    </script>
<?php
}
$conn->close();
?>

list.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BBS demo</title>
    <style>
        table{margin: 0px; border-collapse: collapse; width: 70%;}
        th{border: solid 1px; }
        tr,td{text-align:center; border: solid 1px; padding: 10px }
        .title{width: 50%;}
    </style>
</head>
<body>
    <h1>BBS</h1>
    <table id="table">
        <thead class="head">
            <tr>
                <th>번호</th>
                <th>제목</th>
                <th>작성자</th>
                <th>작성일</th>
                <th>조회수</th>
            </tr>
        </thead>
        <tbody>
            <?php
                // mysql 연결
                $host = 'localhost:3306';
                $user = 'root';
                $pw = '52273178';
                $dbName = 'study_db';
                $conn = new mysqli($host, $user, $pw, $dbName);
                $sql = "SELECT * FROM BBS ORDER BY id DESC";
                $result = mysqli_query($conn, $sql);
                while($row = mysqli_fetch_array($result)){
            ?>
                <tr>
                    <td><?php echo $row["id"] ?></td>
                    <td class="title">
                        <a href="./list_detail.php?id=<?php echo $row["id"]?>"><?php echo $row["title"] ?></a>
                    </td>
                    <td><?php echo $row["writer"] ?></td>
                    <td><?php echo $row["publish"] ?></td>
                    <td><?php echo $row["view_cnt"] ?></td>
                </tr>
            <?php
            }
            $conn->close();
            ?>
        </tbody>
    </table>
    <button onclick=location.href="./writing.php">writing</button><br>
</body>
</html>

list_detail.php

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>BBS demo</title>
</head>
<body>
    <?php
    $id=$_GET['id'];
    // mysql 연결
    $host = 'localhost:3306';
    $user = 'root';
    $pw = '52273178';
    $dbName = 'study_db';
    $conn = new mysqli($host, $user, $pw, $dbName);
    $sql = "SELECT * FROM BBS WHERE id=$id";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_array($result)){

    ?>
    <button onclick=location.href="./list.php">list</button>
    <button onclick=location.href="./edit.php">edit</button>
    <div>
       <div>
        <span><?php  echo $row['id']; ?></span>
            <span>제목</span><span><?php  echo $row['title']; ?></span>
       <span>조회수</span><span><?php  echo $row['view_cnt']; ?></span>
       </div>
        <div>
           <span>작성자</span><span><?php  echo $row['writer']; ?></span>
           <span>작성일</span><span><?php  echo $row['publish']; ?></span>
        </div>
       <div>내용</div><div><?php  echo $row['content']; ?></div>

    </div>

    <?php
        }
        $conn->close();
    ?>
</body>
</html>

 

+ edit, detail,paging

+ login, join, session writer, veiw_cnt

Comment