PHP 프로그래밍

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


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

5. 게시글 수정하기

페이지 정보

작성자 관리자 댓글 0건 조회 549회 작성일 22-01-13 21:34

본문

5. 게시글 수정하기

게시글 수정은 글보기에서 수정으로 연결된다. 연결시 no값을 같이 보낸다.



10.PNG


파일 : write.php


<?php

    require_once("./dbConnect.php");    

    //$_GET['no']이 있을때만 $no변수를 사용할 수 있도록 한다.

    if(isset($_GET['no'])){

        $no=$_GET['no'];

    }

    

    if(isset($no)){

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

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

        $row = $result->fetch_array();

    }

    

?>


<!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">

                    <?php

/* 

hidden으로 no를 처리한 이유는 form에서 submit을 했을 때 게시글 번호도 함께 전송하기 위해서 입니다.

즉, DB수정하고자 하는 글번호를 알 수 있도록 하기 위함.

*/

                    if(isset($no)){

                        echo '<input type="hidden" name="no" value="'.$no.'">';

                    }

                    ?>

                    <table id="boardWrite">

                        <tbody>

                            <tr>

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

                                <td class="id">

                                    <?php

                                        if(isset($no)){

                                            print $row['b_id'];

                                        }else{?>

                                        <input type="text" name="ID" id="ID">

                                        <?php 

                                        } 

                                        ?>

                                </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="<?= isset($row['b_subject'])?$row['b_subject']:null?>">

                                </td>

                            </tr>

                            <tr>

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

                                <td class="content"><textarea name="content" id="content"><?=isset($row['b_content'])?$row['b_content']:null?></textarea></td>

                            </tr>

                        </tbody>

                    </table>

                    <div class="btnSet">

                        <button type="submit" class="submitBtn">

                            <?= isset($no)?'수정하기':'등록하기'?>

                        </button>

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

                    </div>

                </form>

            </div>

        </article>

            

    </body>

</html>




12.PNG

수정 처리를 위해 postWrite.php파일을 수정한다.


<?php

require_once('./dbConnect.php'); 

//$_POST['no'] 있으면 $no를 생성한다.

if(isset($_POST['no'])){

$no = $_POST['no'];

}


//$no 없으면 수정이 아니라 글쓰기 처리를 한다.

if(empty($no)){

$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'];


//글수정 로직

if(isset($no)){

//입력할 비밀번호와 DB에 있는 비밀번호가 일치하는 체크

$sql = 'select count(b_pw) as cnt from bbs where b_pw=password("'.$pw.'") and b_no ='. $no;

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

$row = $result->fetch_array();

//비밀번호가 일치하면 수정 쿼리 작성을 통해 수정처리한다.

if($row['cnt']){

//update sql 작성하기

$sql ='update bbs set b_subject="'.$subject.'",b_content="'.$content.'" where b_no='.$no;

$msgState = '수정';

//echo $sql;

}else{

//비밀번호가 일치하지 않으면 $row['cnt']값은 0이 된다.

$msg='비밀번호가 일치하지 않습니다!!';            

?>

<script>

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

history.back();

</script>

<?php

exit; // exit 처리를 하지 않으면 프로그램이 끝까지 실행된다.

}


// 글등록 로직    

}else{    


//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.'"))';

$msgState = '등록';

}


//메시지가 없을 경우(비밀번호가 일치하는 경우)

if(empty($msg)){

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


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

if($result){

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

if(empty($no)){

$no = $db->insert_id;

}

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

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

}else{

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

?>

<script>

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

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

</script>

<?php

exit;

}

}   

?>


<script>

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

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

</script>



비밀번호와 수정내용을 입력하고 확인을 누르면 게시글이 수정된다.


13.PNG


댓글목록

등록된 댓글이 없습니다.


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

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

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