PHP 프로그래밍

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


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

6. 게시글 삭제하기

페이지 정보

작성자 관리자 댓글 0건 조회 521회 작성일 22-01-13 22:11

본문

6. 게시글 삭제하기

게시글 보기에서 삭제 링크를 통해 삭제화면으로 넘어간다. 삭제화면에서 비밀번호를 입력하여 삭제처리로 보낸다.


파일 : delete.php


<?php

    require_once('dbConnect.php');

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

        $no = $_GET['no'];

    }

?>

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>반응형게시판</title>

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

    </head>

    <body>

        <h3>글 삭제</h3>        

        <?php

            if(isset($no)){

                $sql = 'select count(b_no) as cnt from bbs where b_no='.$no;

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

                $row = $result->fetch_array();

                if(empty($row['cnt'])){

        ?>

    <script>

        alert('삭제할 글이 존재하지 않습니다!!');

        history.back();

    </script>

    <?php

        exit;

                }

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

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

                $row = $result->fetch_array();

    ?>

    <div id='boardDelete'>

        <form action='./postDel.php' method='post'>

            <input type='hidden' name='no' value='<?=$no?>'>

            <table>

                <thead>

                    <tr>

                        <th scope-='col' colspan="2">게시글 삭제하기</th>

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <th scope="row">제목</th>

                        <td><?=$row['b_subject']?></td>

                    </tr>

                    <tr>

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

                        <td><input type='password' name='pw' id='pw'></td>

                    </tr>

                </tbody>

            </table>

            <div class='btnSet'>

                <button type='submit' class='btnSubmit'>삭제</button>

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

            </div>

        </form>

    </div>

    <?php

        // $no가 없는 경우

            }else{

     ?>

        <script>

            alert('정상적인 경로가 아닙니다!!!');

            history.back();

        </script>

    <?php

            exit;

            }

    ?>

    </body>

</html>


삭제화면의 스타일을 추가한다.


/*글삭제*/

#boardDelete table { 

width: 720px; border:1px solid #666;

margin-bottom:10px;

}

#boardDelete tbody th { 

width: 100px; padding:5px;

text-align:right;vertical-align:top;

}

.btnSet { 

width: 720px; text-align:center;

}


삭제 처리는 postDel.php 파일에서 처리합니다.


파일 : postDel.php


<?php

    require_once('dbConnect.php');

    

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

        $no = $_POST['no'];

    }

    

    $pw = $_POST['pw'];

    

    if(isset($no)){

        $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']) {

            $sql = 'delete from bbs where b_no = '.$no;

        }else{

        // 비밀번호 불일치하면 메세지 출력

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

        ?>

        <script>

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

            history.back();

        </script>

        <?php

            exit;

        }

    }

    

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

    

    // 정상적인 쿼리 실행 후

    if($result){

        $msg = '해당 글이 삭제 처리 되었습니다.';

        $replaceURL='./index.php';

    }else{

        $msg = '글 삭제 오류 발생';

     ?>

        <script>

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

            history.back();

        </script>

        <?php

        exit;

    }

?>

        <script>

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

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

        </script>




14.PNG


비밀번호를 입력하고 삭제 버튼을 누른다. 삭제후에는 목록화면으로 이동한다.


댓글목록

등록된 댓글이 없습니다.


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

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

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