[PHP] Book Blog(Books) 만들기 3_베스트셀러

2022. 6. 2. 16:44·Stack/PHP

gallery_board.php

<?php include_once 'include/header.php' ?>
<?php
    $conn = mysqli_connect('localhost', 'root', '0000', '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/books_t/images/{$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_detail.php

<?php include_once 'include/header.php' ?>
<?php 
    // 아이디 값이 일치하는 레코드를 조회
    $conn = mysqli_connect('localhost', 'root', '0000', 'test');
    $query = "select * from galleryBoard where id='{$_GET['id']}'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_array($result);
?>
<div id="best_book" class="inner">
        <h2><?=$row['title']?></h2>
        <h3>베스트셀러 내용입니다.</h3>
        <table>
            <tr>
                <td class="tdcenter">
                    <img src="/php/books_t/images/<?=$row['imgsrc']?>" width="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">
                    <button>수정</button>
                    <form action="process/gallery_delete_process.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_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/gallery_create_process.php" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>글쓴이</td>
                    <td><input type="text" name="writer" required></td>
                </tr>
                <tr>
                    <td>책표지</td>
                    <td><input type="file" name="img" required></td>   <!-- file 쓰려면form에 속성 추가 --> 
                </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>   <!-- type number로 하거나 -->
                </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="desc" cols="30" rows="10"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="submit">도서등록</button>
                        <button type="reset">취소</button>
                    </td>
                </tr>
            </table>
        </form>
        <script>
            let priceInput = document.querySelector("#priceInput");
            priceInput.addEventListener('input', function(){
                if(isNaN(Number(priceInput.value))) {
                    alert("가격은 숫자만 입력해주세요.");
                    priceInput.value="";
                }
            })
        </script>
</div>
<?php include_once 'include/footer.php' ?>

 

gallery_create_process.php

<?php
    // var_dump($_FILES);
    // var_dump($_POST);

    // $file_tmp = $_FILES['img']['tmp_name'];
    // // move_uploaded_file(현재위치, 업로드할 위치);
    // move_uploaded_file($file_tmp,"C:\Apache24\htdocs\PHP\books_t\img/".$_FILES['img']['name']);

    $fileimg = $_FILES['img'];
    // move_uploaded_file(현재위치, 업로드할 위치);
    move_uploaded_file($fileimg['tmp_name'],"C:\Apache24\htdocs\PHP\books_t\images/".$fileimg['name']);

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

 

* 삭제

gallery_delete_process.php

<?php
    $conn = mysqli_connect('localhost', 'root', '0000', 'test');
    $query = "delete from galleryBoard where id={$_POST['id']}";
    $result = mysqli_query($conn, $query);
    // $result2 = mysqli_query($conn, "select from galleryBoard where id={$_POST['id']}");
    // $row = mysqli_fetch_array($result2);
    if($result) {
        unlink("../images/".$_POST['imgsrc']);   // $row['imgsrc']
        echo "삭제되었습니다.";
    } else {
        echo "실패했습니다.";
    }
    header('Location:../gallery_board.php');
?>

 

'Stack > PHP' 카테고리의 다른 글

[PHP] 간단한 신간도서 소개페이지 구현 과제  (0) 2022.07.06
[PHP] Book Blog(Books) 만들기 2_도서등록 / 자세히보기 / 수정 / 삭제  (0) 2022.06.02
[PHP] Book Blog(Books) 만들기_첫 화면 / 회원가입 / 로그인 / 로그아웃 / 검색  (0) 2022.06.02
[PHP] 이미지 업로드  (0) 2022.06.02
[PHP] 쿠키 / 세션  (0) 2022.05.30
'Stack/PHP' 카테고리의 다른 글
  • [PHP] 간단한 신간도서 소개페이지 구현 과제
  • [PHP] Book Blog(Books) 만들기 2_도서등록 / 자세히보기 / 수정 / 삭제
  • [PHP] Book Blog(Books) 만들기_첫 화면 / 회원가입 / 로그인 / 로그아웃 / 검색
  • [PHP] 이미지 업로드
7ingout
7ingout
  • 7ingout
    Hello, 7ingout world!
    7ingout
  • 전체
    오늘
    어제
    • 분류 전체보기 (205)
      • Project (5)
      • Stack (173)
        • React (40)
        • JavaScript (50)
        • TypeScript (14)
        • HTML (11)
        • CSS (31)
        • Spring (9)
        • PHP (15)
        • SQL (3)
        • Python (0)
      • ETC (9)
      • Design (13)
        • Illustrator (6)
        • Photoshop (7)
      • Articloid (4)
        • 7ingout (4)
  • 공지사항

    • ☻
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
7ingout
[PHP] Book Blog(Books) 만들기 3_베스트셀러
상단으로

티스토리툴바