PHP 프로그래밍

본문 바로가기
사이트 내 전체검색


PHP 프로그래밍
PHP 프로그래밍

3. 게시판 글쓰기/글보기 구현

페이지 정보

작성자 관리자 댓글 0건 조회 530회 작성일 22-01-13 20:55

본문

3. 게시판 글쓰기/글보기 구현

파일: write.php


<?php

    require_once("./dbConnect.php");    

?>

<!DOCTYPE html>

<html>

    <head>

        <title>게시판</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="./css/style1.css"/>

    </head>

    <body>

        <article class="boardArticle">

            <h3>글쓰기</h3>

            <div id="write">

                <form action="./postWrite.php" method="post">

                    <table id="boardWrite">

                        <tbody>

                            <tr>

                                <th scope="row"><label for="ID">아이디</label></th>

                                <td class="id"><input type="text" name="ID" id="ID"> </td>

                            </tr>

                            <tr>

                                <th scope="row"><label for="password">비밀번호</label></th>

                                <td class="pw"><input type="text" name="password" id="password"></td>

                            </tr>

                            <tr>

                                <th scope="row"><label for="subject">제목</label></th>

                                <td class="subject">

                                   <input type="text" name="subject" id="subject" value="">

                                </td>

                            </tr>

                            <tr>

                                <th scope="row"><label for="content">내용</label></th>

                                <td class="content"><textarea name="content" id="content"></textarea></td>

                            </tr>

                        </tbody>

                    </table>

                    <div class="btnSet">

                        <button type="submit" class="submitBtn">등록하기</button>

                        <a href="./index.php" class="btnList">목록으로</a>

                    </div>

                </form>

            </div>

        </article>

    </body>

</html>




6.PNG


글쓰기 관련 스타일을 추가한다.


파일: css/style1.css


.boardArticle table { 

width:720px;

border-collapse: collapse;

border-top:2px solid #777; 

}


/*글목록*/

#list th {

padding:5px 0; border:1px solid #777;

}


#list td {

padding:8px; border:1px solid #777;

}


#list .no {

width:60px; text-align:center;

}

#list .subject {

}

#list .author {

width:100px; text-align:center;

}

#list .date {

width:100px; text-align:center;

}


#list .hit {

width:40px; text-align:center;

}

#list .btnSet{

width:720px; text-align:right;

}


/*글쓰기*/

#write table{

width:720px;

}

#write th{

width:100px; padding:5px; text-align:right; vertical-align:top;

}

#write td{ 

width:620px; padding:5px;

}

#write #id{

width:180px;

}

#write #password {

width:180px;

}

#write #subject {

width:400px;

}

#write #content {

width:500px; height:150px;

}

#write .btnSet{

width:720px; text-align:center;

}



7.PNG


글쓰기 처리를 php로 구현한다.

화면에 보이는 웹페이지는 없고, DB 테이블에 데이터를 저장하는 기능을 구현한다.


파일 : postWrite.php


<?php

require_once('./dbConnect.php'); 


$id = $_POST['ID'];

$date = date('Y-m-d H:i:s');


$pw = $_POST['password'];

$subject = $_POST['subject'];

$content = $_POST['content'];

// get, post방식과 상관없이 값을 전달 받을 수 있는 변수는 $_REQUEST['']    

//$content = $_REQUEST['content'];


//mysql에는 자체적으로 입력받은 문자열을 해시화 해주는 함수가 있는데 그것이 password('비밀번호')

$sql = 'insert into bbs (b_no, b_subject, b_content, b_date, b_hit, b_id, b_pw)'

. ' values(null, "'.$subject.'","'.$content.'","'.$date.'",0,"'.$id.'",password("'.$pw.'"))';

$result = $db->query($sql);


//쿼리가 정상적으로 실행되었으면

if($result){

$msg = '정상적으로 글이 등록되었습니다!!';

$no = $db->insert_id;

//게시글을 보여주는 페이지로 이동하기 위해 URL을 저장해주는 부문

$replaceURL = './view.php?no='.$no;    

?>

<script>

alert("<?=$msg?>");

location.replace("<?php echo $replaceURL ?>");

</script>

<?

}else{

$msg = '글 등록을 처리 하지 못했습니다.!!';

?>

<script>

alert("<?=$msg?>");

history.back(); // 이전 페이지로 돌아간다.

</script>

<?php

exit;

}

?>


게시글 보기 페이지를 생성한다.


조회한 결과 데이터를 가져오는 방법

 msyqli_fetch_array(), mysqli_fetch_assoc(), mysqli_fetch_row() 세가지 방법

 fetch_array() 를 이용해서 변수 $row에 받아오면, $row['b_no'] 또는 $row[0] 방법으로 결과를 출력한다.

 fetch_assoc()를 이용할 경우에는 $row['b_no']만 가능하다.

 fetch_row()를 이용할 경우에는 $row[0]만 출력이 가능하다.



파일 : view.php


<?php

require_once('./dbConnect.php');


$no =$_GET['no'];


$sql = 'select b_subject, b_content, b_date, b_hit, b_id from bbs where b_no='.$no;

$result = $db->query($sql);

$row = $result->fetch_array();


?>

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>

        <title>게시판</title>

        <link rel="stylesheet" type="text/css" href="/css/style1.css"/>

        <script src="./js/jquery-2.1.3.min.js"></script>

    </head>

    <body>

            <h2>게시글 보기</h2>

            <div id="boardView" >

                <div id="bSubject"><?=$row['b_subject'] ?></div>

                <div id="bInfo">

                    <span id="ID">작성자 : <?= $row['b_id']?></span>

                    <span id="bDate">작성일 : <?=$row['b_date']?></span>

                    <span id='bHit'>조회수 : <?=$row['b_hit']?></span>

                    

                    <div id='bContent'><?=$row['b_content']?></div>

                </div>

            </div>

            <div class="btnSet">

                <a href = './write.php?no=<?=$no?>'>수정</a>

                <a href='./delete.php?no=<?=$no?>'>삭제</a>

                <a href='./'>목록으로</a>

            </div>

    </body>

</html>



글보기 스타일을 추가한다.


/*글보기*/

#boardView #bSubject {

border-bottom:2px solid #666;

}

#boardView #bInfo {

margin:10px 0;

}

#boardView #bContent {

margin:10px 0;

}




글쓰기 테스트를 한다. 글보기까지 잘 넘어가는 것을 확인할 수 있다.


8.PNG


9.PNG


글보기 스타일을 수정한다.


/*글보기*/

#boardView #bSubject {

width:720px;

background:#eeeeee;

line-height:30px;

font-size:18px;

font-weight:bold;

text-align:center

}

#boardView #bInfo {

width:720px;

border:1px solid #666;

margin:10px 0;

padding:10px 10px;

position:relative;

}


#bInfo #ID, #bDate {

margin-right:20px;

}

#bInfo #bHit {

position:absolute;

right:0;

padding-right:5px;

}

#boardView #bContent {

margin:10px 0;

}



10.PNG


댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.