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
노력이 좋아서

<step49>'도서목록, 등록, 수정, 삭제 만들기'

<step49>'도서목록, 등록, 수정, 삭제 만들기'
노력이 좋아서

<step49>'도서목록, 등록, 수정, 삭제 만들기'

2022. 5. 27. 17:31

1) books

index.php

<?php include_once 'include/header.php' ?>
<?php
    define($total,0);
    function printList(){
        $conn = mysqli_connect('localhost', 'root', '0115', 'test');
        $query = "select * from books";
        $result = mysqli_query($conn, $query);
        $total = mysqli_num_rows($result);
        while($row = mysqli_fetch_array($result)){
            echo  "<tr>
                        <td>{$row['id']}</td>
                        <td><a href=\"detail.php?id={$row['id']}\">{$row['제목']}</a></td>
                        <td>{$row['글쓴이']}</td>
                        <td>{$row['출판사']}</td>
                        <td>{$row['가격']}</td>
                        <td>{$row['출판일']}</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>
            </tr>
            <?php printList(); ?>
        </table>
        <div id="searchDiv">
            <span>검색하기</span>
            <select name="" id="">
                <option value="title">제목</option>
                <option value="writer">글쓴이</option>
            </select>
            <input type="text" name="search">
            <button id="searchBtn">검색하기</button>
            <button id="rightBtn"><a href="create.php">도서등록</a></button>
        </div>
    </div>
<?php include_once 'include/footer.php' ?>

header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/style.css">
</head>
<body>
    <div id="wrap">
        <header>
            <h1>Books</h1>
            <ul>
                <li><a href="index.php">home</a></li>
                <li><a href="index.php">도서목록</a></li>
                <li><a href="create.php">도서등록</a></li>
                <li><a href="">도서검색</a></li>
            </ul>
        </header>
        <div id="contents">

footer.php

</div>
        <footer>
            <p>copyright (c) all rights reserved.</p>
            <h1>Blog</h1>
        </footer>
    </div>
</body>
</html>

style.css

@import url('https://fonts.googleapis.com/css2?family=Varela+Round&display=swap');

* { margin: 0; padding: 0; box-sizing: border-box;}
li { list-style: none;}
a { color: inherit; text-decoration: none;}
:root {
    --main-color: mediumseagreen;
}
body {
    font-family: 'Varela Round', sans-serif;
    font-size: 16px;
    line-height: 1.6;
    color: #222;
}
header {
    display: flex;
    justify-content: space-between;
    height: 80px;
    align-items: center;
    padding: 0 30px;
}
header ul {
    display: flex;
}
header ul li {
    padding: 0 20px;
}
#contents {
    background-image: linear-gradient(to right, #f78ca0 0%, #f9748f 19%, #fd868c 60%, #fe9a8b 100%);
    padding: 60px 0;
}
.inner {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    color: #fff;
}
.inner h2 {
    border-bottom: 1px solid #fff;
    margin-bottom: 30px;
    font-size: 36px;
    font-weight: normal;
}
table {
    border-collapse: collapse;
    width: 100%;
    line-height: 46px;
    margin: 20px 0;
    text-align: center;
}
#contents_page table th {
    background: var(--main-color);
}
#contents_page table td {
    border-bottom: 1px solid #fff;
}
#searchDiv {
    padding: 30px;
}
#searchDiv span {
    padding-right: 50px;
}
#searchDiv input {
    width: 300px;
    line-height: 30px;
    border: none;
    outline: none;
}
#searchDiv #searchBtn {
    background: var(--main-color);
    width: 100px;
    color: #fff;
    border: none;
    outline: none;
    line-height: 30px;
}
#searchDiv #rightBtn {
    float: right;
    width: 100px;
    color: #222;
    border: none;
    outline: none;
    background: #fff;
    text-align: center;
    line-height: 30px;
}
footer {
    padding: 50px;
}

#write_book table td {
    border-bottom: 1px solid #fff;
    padding: 8px;
}
#write_book table td:nth-child(1) {
    width: 30%;
}
#write_book table td:nth-child(2) {
    width: 70%;
    text-align: left;
}
#write_book input, #write_book textarea {
    width: 80%;
    line-height: 30px;
    padding-left: 20px;
    border: none;
    outline: none;
}
#write_book button {
    background: #fff;
    width: 120px;
    border-radius: 6px;
    border: none;
    outline: none;
    text-align: center;
    color: #222;
    line-height: 40px;
}
select {
    width: 100px;
    height: 28px;
    border: none;
    outline: none;
}

create.php

<?php include_once 'include/header.php' ?>
    <div id="write_book" class="inner">
        <h2>도서등록</h2>
        <h3>새로운 도서를 등록하세요.</h3>
        <form action="process/process_create.php" method="post">
            <table>
                <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' ?>

detail.php

<?php include_once 'include/header.php' ?>
    <?php
        $bookid = $_GET['id'];
        $conn = mysqli_connect('localhost', 'root', '0115', 'test');
        $query = "select * from books 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><?=$row['글쓴이']?></td>
            </tr>
            <tr>
                <td>출판사</td>
                <td><?=$row['출판사']?></td>
            </tr>
            <tr>
                <td>출판일자</td>
                <td><?=$row['출판일']?></td>
            </tr>
            <tr>
                <td>제목</td>
                <td><?=$row['제목']?></td>
            </tr>
            <tr>
                <td>내용</td>
                <td><?=$row['desc']?></td>
            </tr>
            <tr>
                <td colspan="2">
                    <button><a href="edit.php?id=<?=$_GET['id']?>">수정</a></button>
                    <form action="process/process_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' ?>

edit.php

<?php include_once 'include/header.php' ?>
    <?php
        $bookid = $_POST['id'];
        $conn = mysqli_connect('localhost', 'root', '0115', 'test');
        $query = "select * from books 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_edit.php?id=<?=$_GET['id']?>" method="post">
            <table>
                <tr>
                    <td>글쓴이</td>
                    <td><input type="text" name="writer" value="<?=$row['글쓴이']?>"></td>
                </tr>
                <tr>
                    <td>출판사</td>
                    <td><input type="text" name="publisher" value="<?=$row['출판사']?>"></td>
                </tr>
                <tr>
                    <td>가격</td>
                    <td><input type="text" name="price" value="<?=$row['가격']?>"></td>
                </tr>
                <tr>
                    <td>출판일</td>
                    <td><input type="text" name="bookdate" value="<?=$row['출판일']?>"></td>
                </tr>
                <tr>
                    <td>제목</td>
                    <td><input type="text" name="title" value="<?=$row['제목']?>"></td>
                </tr>
                <tr>
                    <td>책내용</td>
                    <td><textarea name="desc" id="desc" cols="30" rows="10"><?=$row['desc']?></textarea></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="hidden" value="<?=$bookid?>" name="id">
                        <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>
<?php include_once 'include/footer.php' ?>

process_create.php

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

process_delete.php

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

process_edit.php

<?php
    //post전송으로 넘어온 데이터는 슈퍼글로벌 $_POST변수가 배열형태로 받는다.
    $conn = mysqli_connect('localhost', 'root', '0115', 'test');
    //데이터 수정은 update문 사용
    // update 테이블 이름
    // set 컬럼명 = "변경될 값"
    // where 컬럼
    $query = "UPDATE books SET `제목` = '{$_POST['title']}', `글쓴이` = '{$_POST['writer']}',  `출판사` = '{$_POST['publisher']}', `가격` = {$_POST['price']},
     `출판일` = '{$_POST['bookdate']}',`desc` = '{$_POST['desc']}' WHERE `id` = '{$_POST['id']}'";
    echo $query;
    $result = mysqli_query($conn, $query);
    header('Location:../index.php');
?>

도서목록
도서등록
도서 자세히보기, 삭제
도서수정

 

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

<step50>'php_세션'  (0) 2022.05.30
<step50>'도서목록 페이징'  (0) 2022.05.30
<step48>'php_sql연동'  (0) 2022.05.26
<step48>'php_Blog만들기'  (0) 2022.05.26
<step48>'php_empty(), isset(), include'  (0) 2022.05.26

    티스토리툴바

    개인정보

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

    단축키

    내 블로그

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

    블로그 게시글

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

    모든 영역

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

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