노력이 좋아서

<step52>'베스트 셀러 등록, 수정, 삭제_teach'

zoaseo 2022. 6. 2. 15:17

1)

gallery_index.php

<?php include_once 'include/header.php' ?>
<?php
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "select * from galleryBoard";
    $result = mysqli_query($conn, $query);
    function printList(){
        global $result;
        while($row = mysqli_fetch_array($result)){
            echo "<li><a href='gallery_detail.php?id={$row['id']}'><img src='/php/book_teach/img/{$row['imgsrc']}'></a></li>";
        }
    }
?>
<div id="bestSeller_page" class="inner">
        <h2>베스트 셀러 목록</h2>
        <h3>베스트 셀러 목록입니다. </h3>
        <ul>
            <?php printList(); ?>
        </ul>
</div>
<?php include_once 'include/footer.php' ?>

gallery_create.php

<?php include_once 'include/header.php' ?>
    <div id="write_book" class="inner">
        <h2>베스트 셀러 등록</h2>
        <h3>베스트 셀러를 등록하세요.</h3>
        <!-- 파일을 업로드할 경우 form 속성 추가하기
        enctype="multipart/form-data" -->
        <form action="process/process_gallery_create.php" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>책표지</td>
                    <td><input type="file" name="img" required></td>
                </tr>
                <tr>
                    <td>글쓴이</td>
                    <td><input type="text" name="writer" required></td>
                </tr>
                <tr>
                    <td>출판사</td>
                    <td><input type="text" name="publisher" required></td>
                </tr>
                <tr>
                    <td>가격</td>
                    <td><input type="text" name="price" required id="priceInput"></td>
                </tr>
                <tr>
                    <td>출판일</td>
                    <td><input type="text" name="bookdate"></td>
                </tr>
                <tr>
                    <td>제목</td>
                    <td><input type="text" name="title"></td>
                </tr>
                <tr>
                    <td>책내용</td>
                    <td><textarea name="desc" id="" cols="30" rows="10"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button>도서등록</button>
                        <button>취소</button>
                    </td>
                </tr>
            </table>
        </form>
        <script>
            let priceInput = document.querySelector('#priceInput');
            priceInput.addEventListener('input', function(){
                if(isNaN(Number(priceInput.value))){
                    alert("가격은 숫자만 입력해주세요.");
                    priceInput.value="";
                }
            })
        </script>
<?php include_once 'include/footer.php' ?>

gallery_detail.php

<?php include_once 'include/header.php' ?>
<?php
    // 아이디 값이 일치하는 레코드를 조회
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "select * from galleryBoard where id='{$_GET['id']}'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_array($result);
?>
<div id="write_book" class="inner">
        <h2><?=$row['title']?></h2>
        <h3>베스트 셀러 내용입니다.</h3>
        <table>
            <tr>
                <td class="tdcenter">
                    <img src="/php/book_teach/img/<?=$row['imgsrc']?>" witdth="400">
                </td>
                <td>
                    <ul>
                        <li>출판사 : <?=$row['publisher']?></li>
                        <li>글쓴이 : <?=$row['writer']?></li>
                        <li>출판일자 : <?=$row['bookdate']?></li>
                        <li>가격 : <?=$row['price']?></li>
                    </ul>
                </td>
            </tr>
            <tr>
                <td colspan="2">책소개</td>
            </tr>
            <tr>
                <td colspan="2"><?=$row['desc']?></td>
            </tr>
            <tr>
                <td colspan="2">
                    <form action="">
                        <button>수정</button>
                    </form>
                    <form action="process/process_gallery_delete.php" method="post">
                        <input type="hidden" name="id" value=<?=$_GET['id']?>>
                        <input type="hidden" name="imgsrc" value="<?=$row['imgsrc']?>">
                        <button type="submit">삭제</button>    
                    </form>
                </td>
            </tr>
        </table>
</div>
<?php include_once 'include/footer.php' ?>

gallery_edit.php

<?php include_once 'include/header.php' ?>
<?php
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "select * from galleryBoard where id = {$_POST['id']}";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_array($result);
?>
    <div id="write_book" class="inner">
        <h2>베스트 셀러 도서 수정</h2>
        <h3>베스트 셀러 도서를 수정하세요.</h3>
        <!-- 파일을 업로드할 경우 form 속성 추가하기
        enctype="multipart/form-data" -->
        <form action="process/process_gallery_edit.php" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>책표지</td>
                    <td>
                        <input type="hidden" name="oldimg" value="<?=$row['imgsrc']?>">
                        <input type="file" name="img" required style="position:absolute; opacity:0;" onchange="imageChange(this)">
                        <label class="file">파일선택</label>
                        <label id="imglabel"><?=$row['imgsrc']?></label>
                    </td>
                </tr>
                <tr>
                    <td>글쓴이</td>
                    <td>
                        <input type="text" name="writer" required value="<?=$row['writer']?>">
                        <input type="hidden" name="id" value="<?=$row['id']?>">
                    </td>
                </tr>
                <tr>
                    <td>출판사</td>
                    <td><input type="text" name="publisher" required value="<?=$row['publisher']?>"></td>
                </tr>
                <tr>
                    <td>가격</td>
                    <td><input type="text" name="price" required id="priceInput" value="<?=$row['price']?>"></td>
                </tr>
                <tr>
                    <td>출판일</td>
                    <td><input type="text" name="bookdate" value="<?=$row['bookdate']?>"></td>
                </tr>
                <tr>
                    <td>제목</td>
                    <td><input type="text" name="title" value="<?=$row['title']?>"></td>
                </tr>
                <tr>
                    <td>책내용</td>
                    <td><textarea name="desc" id="" cols="30" rows="10"><?=$row['desc']?></textarea></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="submit">도서수정</button>
                        <button>취소</button>
                    </td>
                </tr>
            </table>
        </form>
        <script>
            function imageChange(input) {
                // #imglabel의 내용을 input의 value값으로 변경
                // 배열의 마지막 인덱스 배열길이 - 1
                if(input.value){
                    let valueArr = input.value.split('\\');
                    document.querySelector('#imglabel').innerHTML = valueArr[valueArr.length - 1];
                }else {
                    document.querySelector('#imglabel').innerHTML = '선택 파일 없음';
                }
            }
            let priceInput = document.querySelector('#priceInput');
            priceInput.addEventListener('input', function(){
                if(isNaN(Number(priceInput.value))){
                    alert("가격은 숫자만 입력해주세요.");
                    priceInput.value="";
                }
            })
        </script>
<?php include_once 'include/footer.php' ?>

process_gallery_create.php

<?php
    $file = $_FILES['img'];
    // move_uploaded_file(현재위치, 업로드할 위치);
    move_uploaded_file($file['tmp_name'], 'C:Apache24/htdocs/php/book_teach/img/'.$file['name']);

    //post전송으로 넘어온 데이터는 슈퍼글로벌 $_POST변수가 배열형태로 받는다.
    $conn = mysqli_connect('localhost', 'root', '0115', 'test');
    $query = "INSERT INTO galleryBoard (`title`, `writer`, `publisher`, `price`, `bookdate`, `desc`, `imgsrc`) 
    values('{$_POST['title']}', '{$_POST['writer']}', '{$_POST['publisher']}', {$_POST['price']}, '{$_POST['bookdate']}', '{$_POST['desc']}', '{$file['name']}');";
    echo $query;
    $result = mysqli_query($conn, $query);
    if($result){
        echo "게시글을 작성했습니다.";
    }else {
        echo "게시글 작성을 실패했습니다.";
    }
    header('Location:../gallery_index.php');

?>

process_gallery_delete.php

<?php
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "delete from galleryBoard where id={$_POST['id']}";
    $result = mysqli_query($conn, $query);
    if($result) {
        unlink("../img/".$_POST['imgsrc']);  // img의 파일도 같이 삭제
        echo "삭제되었습니다.";
    }else {
        echo "실패했습니다.";
    }
    header('Location:../gallery_index.php');
?>

process_gallery_edit.php

<?php
    $imgsrc = $_FILES ? $_FILES['img']['name'] : $_POST['oldimg'];
    if($_FILES['img']['name']!=$_FILES['oldimg']){
        // 책표지가 변경되면 이전파일을 삭제
        // 변경된 파일을 이미지 폴더로 업로드
        unlink('../img/'.$_POST['oldimg']);
        move_uploaded_file($_FILES['img']['tmp_name'], 'C:Apache24/htdocs/php/book_teach/img/'.$_FILES['img']['name']);
    }
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "UPDATE galleryBoard SET `imgsrc` = '{$imgsrc}', `title` = '{$_POST['title']}', `writer` = '{$_POST['writer']}',  `publisher` = '{$_POST['publisher']}', `price` = {$_POST['price']},
    `bookdate` = '{$_POST['bookdate']}' WHERE `id` = '{$_POST['id']}'";
    $result = mysqli_query($conn, $query);
    if($result){
        echo "성공";
    }else{
        echo "실패";
        echo $query;
    }
    header('Location:../gallery_index.php');
?>

베스트 셀러 등록
베스트 셀러 목록
베스트 셀러 자세히보기, 수정, 삭제
베스트 셀러 수정