zoaseo
To Infinity And Beyond
zoaseo
전체 방문자
오늘
어제
  • 분류 전체보기 (763)
    • 개발이 좋아서 (381)
      • SAP가 좋아서 (3)
      • Java가 좋아서 (42)
      • Spring이 좋아서 (50)
      • JPA가 좋아서 (0)
      • QueryDSL이 좋아서 (26)
      • Docker가 좋아서 (7)
      • Redis가 좋아서 (7)
      • AWS가 좋아서 (5)
      • CI/CD가 좋아서 (6)
      • Troubleshooting이 좋아서 (4)
      • Kotlin이 좋아서 (7)
      • SQL이 좋아서 (6)
      • HTTP가 좋아서 (21)
      • JavaScript가 좋아서 (30)
      • TypeScript가 좋아서 (6)
      • Vue가 좋아서 (21)
      • Flutter가 좋아서 (61)
      • React가 좋아서 (20)
      • Redux(React)가 좋아서 (2)
      • Angular가 좋아서 (22)
      • HTML이 좋아서 (9)
      • CSS가 좋아서 (15)
      • PHP가 좋아서 (9)
      • Illustrator가 좋아서 (2)
    • 노력이 좋아서 (169)
    • 결과물이 좋아서 (14)
    • 코딩연습이 좋아서 (168)
      • 이론이 좋아서 (62)
      • SQL이 좋아서 (90)
    • 유용한 사이트가 좋아서 (28)
    • Github (2)

인기 글

티스토리

hELLO · Designed By 정상우.
zoaseo
노력이 좋아서

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

<step52>'베스트 셀러 등록, 수정, 삭제'
노력이 좋아서

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

2022. 6. 2. 14:50

1) 베스트 셀러 등록, 수정, 삭제

best.php

<?php include_once 'include/header.php' ?>
<?php
    define($total, 0);
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "select * from bestsell";
    $result = mysqli_query($conn, $query);
    $total = mysqli_num_rows($result);
    // echo $total;

    // 한 페이지당 레코드 개수
    $list_num = 8;

    // 한 블럭당 페이지 수
    $page_num = 2;

    // 현재 페이지
    $page = isset($_GET['page']) ? $_GET['page'] : 1;

    // 전체 페이지수 =  전체 레코드 수 / 한 페이지당 레코드 개수
    // 전체 데이터가 20개라 하면, 20 / 5 => 4
    $total_page = ceil($total / $list_num);

    // 전체 블럭수 = 전체 페이지 수 / 블럭당 페이지 수
    $total_block = ceil($total_page / $page_num);

    // 현재 블럭 번호 = 현재 페이지 번호/ 블럭당 페이지 수
    // 1 / 3  1   7 / 3
    $now_block = ceil($page / $page_num);

    // 블럭 당 시작 페이지 번호 = (해당글의 블럭번호 -1) * 블럭당 페이지수 + 1
    $s_pageNum = ($now_block -1 ) * $page_num+1;

    // 데이터가 0개인 경우
    if($s_pageNum <= 0) {
        $s_pageNum = 1;
    }

    // 블럭 당 마지막 페이지 번호
    $e_pageNum = $now_block * $page_num;

    // 마지막 페이지 번호가 전체 페이지 수를 넘지 않도록 설정
    if($e_pageNum > $total_page) {
        $e_pageNum = $total_page;
    }

    // 시작번호 0, 5, 10, 15 ...
    $start = ($page-1) * $list_num;

    // 쿼리작성
    $sql = "select * from bestsell limit $start, $list_num;";
    $result2 = mysqli_query($conn, $sql);
   
    function printList() {
        global $result2;
        global $total;
        while($row = mysqli_fetch_array($result2)){
            echo "<tr>
                    <td>{$row['id']}</td>
                    <td><img src=.//{$row['imgroot']}></td>
                    <td><a href=\"best_detail.php?id={$row['id']}\">{$row['imgname']}</a></td>
                    <td>{$row['writer']}</td>
                    <td>{$row['publisher']}</td>
                    <td>{$row['price']}원</td>
                    <td>{$row['bookdate']}</td>
                </tr>";
        }
    }
?>
    <div id="contents_page" class="inner">
        <h2>베스트 셀러 목록</h2>
        <h3>베스트 셀러 목록입니다. </h3>
        <table>
            <tr>
                <th>아이디</th>
                <th>이미지</th>
                <th>제목</th>
                <th>글쓴이</th>
                <th>출판사</th>
                <th>가격</th>
                <th>출판일</th>
            </tr>
            <?php printList(); ?>
        </table>
        <p class="pager">
        <!-- 이전버튼 -->
        <?php
            if($page <= 1){ ?>
            <a href="best.php?page=1">이전</a>
        
        <?php
            }
            else{ ?>
            <a href="best.php?page=<?=($page-1)?>">이전</a>
        <?php
            }
        ?>
        <?php
            for($print_page = $s_pageNum; $print_page <= $e_pageNum; $print_page++) {
        ?>
            <a href="best.php?page=<?=$print_page?>"><?=$print_page?></a>
        <?php
            }
        ?>
        <!-- 다음버튼 -->
        <?php
            if($page >= $total_page){
        ?>
            <a href="best.php?page=<?=$total_page?>">다음</a>
        <?php
            }else{
        ?>
            <a href="best.php?page=<?=($page+1)?>">다음</a>
        <?php
            }
        ?>
        </p>
        <div id="searchDiv">
            <form action="search.php" method="post">
                <span>검색하기</span>
                <select name="search_m" id="search">
                    <option value="title">제목</option>
                    <option value="writer">글쓴이</option>
                    <option value="publisher">출판사</option>
                </select>
                <input type="text" name="search" required>
                <button id="searchBtn">검색하기</button>
            </form>
            <button id="rightBtn"><a href="best_create.php">도서등록</a></button>
        </div>  
    </div>
<?php include_once 'include/footer.php' ?>

best_create.php

<?php include_once 'include/header.php' ?>
    <div id="write_book" class="inner">
        <h2>베스트 셀러 등록</h2>
        <h3>베스트 셀러를 등록하세요.</h3>
        <form action="process/process_best_create.php" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>이미지</td>
                    <td><input type="file" name="imgroot" 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="imgname"></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' ?>

best_detail.php

<?php include_once 'include/header.php' ?>
    <?php
        $bookid = $_GET['id'];
        $conn = mysqli_connect('localhost', 'root', '0115', 'test');
        $query = "select * from bestsell where id={$bookid}";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_array($result);
    ?>
    <div id="write_book" class="inner">
        <h2>베스트 셀러 자세히보기</h2>
        <h3>베스트 셀러 내용입니다.</h3>
        <table>
            <tr>
                <td>글쓴이</td>
                <td><img src=.//<?=$row['imgroot']?>></td>
            </tr>
            <tr>
                <td>글쓴이</td>
                <td><?=$row['writer']?></td>
            </tr>
            <tr>
                <td>출판사</td>
                <td><?=$row['publisher']?></td>
            </tr>
            <tr>
                <td>출판일자</td>
                <td><?=$row['bookdate']?></td>
            </tr>
            <tr>
                <td>제목</td>
                <td><?=$row['imgname']?></td>
            </tr>
            <tr>
                <td>내용</td>
                <td><?=$row['desc']?></td>
            </tr>
            <tr>
                <td colspan="2">
                    <form action="best_edit.php" method="post">
                    <input type="hidden" name="id" value="<?=$_GET['id']?>">
                    <button type="submit">수정</button>
                    </form>
                    <form action="process/process_best_delete.php" method="post">
                    <input type="hidden" name="id" value="<?=$_GET['id']?>">
                    <button type="submit">삭제</button>
                    </form>
                </td>
            </tr>
        </table>
    </div>
<?php include_once 'include/footer.php' ?>

best_edit.php

<?php include_once 'include/header.php' ?>
    <?php
        $bookid = $_POST['id'];
        $conn = mysqli_connect('localhost', 'root', '0115', 'test');
        $query = "select * from bestsell where id={$bookid}";
        $result = mysqli_query($conn, $query);
        $row = mysqli_fetch_array($result);
    ?>
    <div id="write_book" class="inner">
        <h2>도서수정</h2>
        <h3>도서를 수정하세요.</h3>
        <form action="process/process_best_edit.php?id=<?=$_POST['id']?>" method="post">
            <table>
                <tr>
                    <td>글쓴이</td>
                    <td><input type="text" name="writer" value="<?=$row['writer']?>"></td>
                </tr>
                <tr>
                    <td>출판사</td>
                    <td><input type="text" name="publisher" value="<?=$row['publisher']?>"></td>
                </tr>
                <tr>
                    <td>가격</td>
                    <td><input type="text" name="price" 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="imgname" value="<?=$row['imgname']?>"></td>
                </tr>
                <tr>
                    <td>책내용</td>
                    <td><textarea name="desc" id="desc" cols="30" rows="10"><?=$row['desc']?></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' ?>

process_best_create.php

<?php
    $file = $_FILES['imgroot'];
    $result = move_uploaded_file($file['tmp_name'],'C:Apache24/htdocs/php/book_teach/img/'.$file['name']);
    $conn = mysqli_connect('localhost','root','0115','test');
    $query = "insert into bestsell(`imgroot`, `imgname`, `writer`, `publisher`, `price`, `bookdate`)
    values('../img/{$file['name']}','{$_POST['imgname']}', '{$_POST['writer']}',
    '{$_POST['publisher']}','{$_POST['price']}','{$_POST['bookdate']}')";
    $result2 = mysqli_query($conn, $query);
    echo $query;
    if($result2){
        echo "성공";
    }else {
        echo "실패";
    }
    header('Location:../best.php');
?>

process_best_delete.php

<?php
    echo $_POST['id'];
    
    $conn = mysqli_connect('localhost', 'root', '0115', 'test');
    $query = "delete from bestsell where id={$_POST['id']}";
    $result = mysqli_query($conn, $query);
    if($result){
        echo "성공";
    }else {
        echo "실패";
    }
    header('Location:../best.php');
?>

process_best_edit.php

<?php
    //post전송으로 넘어온 데이터는 슈퍼글로벌 $_POST변수가 배열형태로 받는다.
    $conn = mysqli_connect('localhost', 'root', '0115', 'test');
    $query = "UPDATE bestsell SET `imgname` = '{$_POST['imgname']}', `writer` = '{$_POST['writer']}',  `publisher` = '{$_POST['publisher']}', `price` = {$_POST['price']},
     `bookdate` = '{$_POST['bookdate']}' WHERE `id` = '{$_GET['id']}'";
    echo $query;
    $result = mysqli_query($conn, $query);
    if($result){
        echo "게시글을 작성했습니다.";
    }else {
        echo "게시글 작성을 실패했습니다.";
    }
    header('Location:../best.php');
?>

베스트 셀러 등록
베스트 셀러 목록
베스트 셀러 수정, 삭제

'노력이 좋아서' 카테고리의 다른 글

<step53>'기프트 샵 만들기'  (0) 2022.06.03
<step52>'베스트 셀러 등록, 수정, 삭제_teach'  (0) 2022.06.02
<step52>'php_이미지 전송'  (0) 2022.06.02
<step51>'과제 실습'  (2) 2022.05.31
<step50>'php_세션,쿠키_실습-도서목록_회원가입, 로그인'  (0) 2022.05.30
    To Infinity And Beyondzoaseo 님의 블로그입니다.

    티스토리툴바

    개인정보

    • 티스토리 홈
    • 포럼
    • 로그인

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.