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>'베스트 셀러 등록, 수정, 삭제_teach'

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

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

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');
?>

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

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

<step54>'mysql_join'  (0) 2022.06.07
<step53>'기프트 샵 만들기'  (0) 2022.06.03
<step52>'베스트 셀러 등록, 수정, 삭제'  (0) 2022.06.02
<step52>'php_이미지 전송'  (0) 2022.06.02
<step51>'과제 실습'  (2) 2022.05.31

    티스토리툴바

    개인정보

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

    단축키

    내 블로그

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

    블로그 게시글

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

    모든 영역

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

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