Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Archives
Today
Total
관리 메뉴

LogicalMaster

PHP 로 간단한 게시판을 만들어보기 본문

카테고리 없음

PHP 로 간단한 게시판을 만들어보기

LogicalMaster 2020. 9. 27. 22:00

안녕하세요.

 

이번엔 PHP 을 이용하여 간단한 게시판을 한번 만들어 보겠습니다.

 

 

- 필요 준비물

PHP, MySQL, NGiNX 서버, phpMyAdmin (DB 관리 툴), 비주얼코드 (Visual Code).

 

이렇게 되겠습니다.

 

 

바로 시작합시다.

 

 

- 먼저 서버 설치 (xampp)

 

다운로드 공식 사이트 : www.apachefriends.org/index.html

 

XAMPP Installers and Downloads for Apache Friends

What is XAMPP? XAMPP is the most popular PHP development environment XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. The XAMPP open source package has been set up to be incredibly easy to install and to us

www.apachefriends.org

- xampp 설치

 

 

- 비주얼코드 (Visual Code) 설치

 

공식 다운로드 홈페이지 : code.visualstudio.com/download

 

Download Visual Studio Code - Mac, Linux, Windows

Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows. Download Visual Studio Code to experience a redefined code editor, optimized for building and debugging modern web and cloud applications.

code.visualstudio.com

 

- 비주얼코드 설치

 

 

- 비주얼코드 실행

 

 

- board_list.php 소스

 

<?php

$host = "localhost";

$user = "example";

$passwd = "example123";

$dbName = "postboard";

 

@$oDB = new mysqli($host$user$passwd$dbName); // DB에 연결

 

if ($oDB->connect_error) { // DB 연결에 실패하면 오류 출력

    die("DB Error : ".$oDB->connect_error); // 오류 출력

}

 

function sql_query($sql='') { // SQL 쿼리 실행 함수

    global $oDB;

 

    return $oDB->query($sql);

}

 

function sql_get_row($sql='') { // SQL 구문 값 가져오는 함수

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_ASSOC);

}

 

function sql_get_value($sql='') { // SQL 구문 값을 가져오나 total 숫자만 출력

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_NUM)[0];

}

?>

<style>

.messageHead {

    border:3px #cccccc solid; // 테두리 설정

    padding:5px; // 폭 설정

    font-size:10pt; // 글자 사이즈 설정

}

 

.boardList {

    font-size:10pt;

    text-align:left;

}

</style>

<div class="messageHead">게시판</div><br/>

<div class="boardList">

<?php

$sql = sql_query("select * from bd__board order by b_idx desc");

 

while ($data = $sql->fetch_assoc()):

?>

    <a href="/?b_idx=<?=$data['b_idx']?>"><?=$data['b_title']?></a><br/>

<?php endwhile?>

</div>

 

 

- 코드 실행

 

데이터베이스가 아직 없어 오류가 난다.

 

phpMyAdmin 을 이용하여 데이터베이스 생성과 테이블 관리를 진행하겠다.

 

 

- phpMyAdmin 접속

 

상단 탭의 "데이터베이스" 선택

 

데이터베이스 화면에서 DB 생성

 

테이블 생성하기

 

bd__board 테이블을 만들고 column 을 6개 만든다.

 

값을 입력하고 우측 하단의 저장 클릭.

 

값이 저장되었다.

 

오류 해결 완료. 하지만 가독성이 매우 낮은 상태이므로 약간의 style 을 추가해준다.

 

 

- 소스

<?php

$host = "localhost"; // DB 호스트

$user = "example"; // DB 사용자

$passwd = "example123"; // DB 사용자 패스워드

$dbName = "postboard"; // 데이터베이스 선택

 

@$oDB = new mysqli($host$user$passwd$dbName);

 

if ($oDB->connect_error) {

    die("DB Error : ".$oDB->connect_error);

}

 

function sql_query($sql='') {

    global $oDB;

 

    return $oDB->query($sql);

}

 

function sql_get_row($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_ASSOC);

}

 

function sql_get_value($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_NUM)[0];

}

?>

<style>

.container {

    width:1080px;

    padding:10px;

    margin0px auto;

}

 

.messageHead {

    border:3px #cccccc solid;

    padding:5px;

    font-size:10pt;

    text-align:center;

}

 

.boardList {

    font-size:10pt;

    text-align:left;

}

</style>

<div class="container">

    <div class="messageHead">게시판</div><br/>

    <div class="boardList">

<?php

$sql = sql_query("select * from bd__board order by b_idx desc");

 

while ($data = $sql->fetch_assoc()):

?>

        <a href="/?b_idx=<?=$data['b_idx']?>"><?=$data['b_title']?></a><br/>

<?php endwhile?>

    </div>

</div>

 

 

- 결과화면

 

글이 하나도 없다.

 

글 작성 페이지를 구현해보자.

 

 

- 소스

<?php

$host = "localhost";

$user = "example";

$passwd = "example123";

$dbName = "postboard";

 

@$oDB = new mysqli($host$user$passwd$dbName);

 

if ($oDB->connect_error) {

    die("DB Error : ".$oDB->connect_error);

}

 

function sql_query($sql='') {

    global $oDB;

 

    return $oDB->query($sql);

}

 

function sql_get_row($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_ASSOC);

}

 

function sql_get_value($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_NUM)[0];

}

?>

<style>

.container {

    width:1080px;

    padding:10px;

    margin0px auto;

}

 

.messageHead {

    border:3px #cccccc solid;

    padding:5px;

    font-size:10pt;

    text-align:center;

}

</style>

<div class="container">

    <div class="messageHead">게시판 작성..</div><br/>

    <form method="post" name="dispBoardWriteForm" id="dispBoardWriteForm" action="./board_write_submit.php" onsubmit="return procFilter('dispBoardWriteFormSubmit')" style="font-size:10pt">

        제 목<br/>

        <input type="text" name="b_title" id="b_title" placeholder="제목을 입력하세요." style="width:720px"/><br/><br/>

        내 용<br/>

        <textarea name="b_contents" id="b_contents" placeholder="내용을 입력하세요." style="width:720px; height:360px"></textarea><br/><br/><br/>

        <center>

            <button type="submit">등록</button>&nbsp;

            <button onclick="history.back(); return false">취소</button>

        </center>

    </form>

</div>

 

 

- 결과화면

 

- board_write_submit.php 소스

<?php

$host = "localhost";

$user = "example";

$passwd = "example123";

$dbName = "postboard";

 

@$oDB = new mysqli($host$user$passwd$dbName);

 

if ($oDB->connect_error) {

    die("DB Error : ".$oDB->connect_error);

}

 

function sql_query($sql='') {

    global $oDB;

 

    return $oDB->query($sql);

}

 

function sql_get_row($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_ASSOC);

}

 

function sql_get_value($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_NUM)[0];

}



if (trim($_POST['b_title']) == "" || trim($_POST['b_contents']) == "") {

    die("<script>alert(\"제목과 내용을 입력해주세요.\"); history.back();</script>");

}



// 게시판 DB에 게시물 데이터 삽입

$sql = "insert into bd__board set

    b_title = '".$_POST['b_title']."',

    b_contents = '".$_POST['b_contents']."'

";

 

// 쿼리가 잘 실행되면 

if (sql_query($sql)) {

    die("<script>alert(\"등록했습니다.\");</script>");

else { // 쿼리가 실행되지 않았을 때

    die("<script>alert(\"등록하지 못했습니다.\n\n서버 오류\"); history.back();</script>");

}

?>

 

 

- 결과화면

제목과 내용을 입력한 후 등록.

 

 

- 결과화면

 

- 게시판 목록

 

- 본문 페이지 만들기 (board_view.php 소스)

<?php

$host = "localhost";

$user = "example";

$passwd = "example123";

$dbName = "postboard";

 

@$oDB = new mysqli($host$user$passwd$dbName);

 

if ($oDB->connect_error) {

    die("DB Error : ".$oDB->connect_error);

}

 

function sql_query($sql='') {

    global $oDB;

 

    return $oDB->query($sql);

}

 

function sql_get_row($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_ASSOC);

}

 

function sql_get_value($sql='') {

    global $oDB;

 

    return $oDB->query($sql)->fetch_array(MYSQLI_NUM)[0];

}

?>

<style>

.container {

    width:1080px;

    padding:10px;

    margin0px auto;

}

 

.messageHead {

    border:3px #cccccc solid;

    padding:5px;

    font-size:10pt;

    text-align:center;

}

</style>

<?php

if ($_GET['b_idx'] == "") {

    die("표시할 글이 없습니다.");

}



$board_data = sql_get_row("select * from bd__board where b_idx = '".addslashes($_GET['b_idx'])."'");

 

if (!$board_data['b_idx']) {

    die("존재하지 않는 글입니다.");

}

?>

<div class="container">

    <h3><?=$board_data['b_title']?></h3><hr/>

    <p><?=$board_data['b_contents']?></p>

</div>

 

 

- 결과화면

 

전체 소스 파일을 첨부합니다.

 

htdocs.zip
0.00MB

 

 

여기까지 입니다.

 

감사합니다.

Comments