Stack/PHP

[PHP] Book Blog(Books) 만들기_첫 화면 / 회원가입 / 로그인 / 로그아웃 / 검색

7ingout 2022. 6. 2. 15:50

index.php

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

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

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

    // 현재 페이지
    $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 books limit $start, $list_num;";
    // echo $sql;
    $result2 = mysqli_query($conn, $sql);
    
    function printList() {
        global $result2;
        global $total;
        // echo $total;
        while($row = mysqli_fetch_array($result2)){
            echo "<tr>
                    <td>{$row['id']}</td>
                    <td><a href=\"detail_t.php?id={$row['id']}\">{$row['title']}</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>
            </tr>
            <?php printList(); ?>
        </table>
        <p class="pager">
        <!-- 이전버튼 -->
        <?php 
            if($page <= 1 ) { ?>
                <a href="index.php?page=1">이전</a>
        <?php
        }
            else { ?>
        <a href="index.php?page=<?=$page-1?>">이전</a>
        <?php    
        }
        ?>
        <?php
            for($print_page = $s_pageNum; $print_page <= $e_pageNum; $print_page++) { 
        ?>
            <a href="index.php?page=<?=$print_page?>"><?=$print_page?></a>
        <?php
            }
        ?>
        <?php
            if($page >= $total_page) {
        ?>
            <a href="index.php?page=<?=$total_page?>">다음</a>
        <?php
            } else {
        ?>
            <a href="index.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="create.php">도서등록</a></button>
        </div>  
    </div>
<?php include_once 'include/footer.php' ?>

 

include/header.php

<?php
    session_start();
?>
<!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="/php/books_t/css/style.css">   <!-- 절대경로 -->
</head>
<body>
    <div id="wrap">
        <header>
            <h1><a href="/php/books_t/index.php">Books</a></h1>
            <ul>
                <li><a href="/php/books_t/index.php">home</a></li>
                <li>
                <?php
                    if(isset($_SESSION['userId'])) {
                        echo "<a href='/php/books_t/process/logout_process.php'>로그아웃</a>";
                    } else {
                        echo "<a href='/php/books_t/member/login.php'>로그인</a>";
                    }
                ?>
                <li><a href="/php/books_t/member/join.php">회원가입</a></li>
                <li><a href="/php/books_t/index.php">도서목록</a></li>
                <li><a href="/php/books_t/create.php">도서등록</a></li>
                <li><a href="/php/books_t/gallery_board.php">베스트셀러목록</a></li>
                <li><a href="/php/books_t/gallery_create.php">베스트셀러등록</a></li>
                <li><a href="">도서검색</a></li>
            </ul>
        </header>
        <div id="contents">

 

include/footer.php

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

 

 

 

member/join.php

<?php include_once '../include/header.php' ?>
<div id="write_book" class="inner">
    <h2>회원가입</h2>
    <h3>회원정보를 입력하세요.</h3>
    <form action="../process/join_process.php" method="post">
        <table>
            <tr>
                <td>아이디</td>
                <td><input type="text" name="userId"></td>
            </tr>
            <tr>
                <td>비밀번호</td>
                <td><input type="password" name="userPw"></td>
            </tr>
            <tr>
                <td>비밀번호체크</td>
                <td><input type="password" name="userPwCh"></td>
            </tr>
            <tr>
                <td>이름</td>
                <td><input type="text" name="userName"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <button type="submit">회원가입</button>
                    <button type="reset">취소</button>
                </td>
            </tr>
        </table>
    </form>
</div>
<?php include_once '../include/footer.php' ?>

 

process/join_process.php

<?php
    $conn = mysqli_connect('localhost', 'root', '0000', 'test');
    $query = "insert into members(id, pw, date, name)
    values('{$_POST['userId']}', '{$_POST['userPw']}', NOW(), '{$_POST['userName']}')";
    $result = mysqli_query($conn, $query);
    if($result) {
        echo "성공";
    } else {
        echo "실패";
    }
    header('Location:../index.php');
?>

 

 

 

member/login.php

<?php include_once '../include/header.php' ?>
<div id="write_book" class="inner">
    <h2>로그인</h2>
    <h3>아이디와 패스워드를 입력하세요.</h3>
    <form action="../process/login_process.php" method="post">
        <table>
            <tr>
                <td>아이디</td>
                <td><input type="text" name="userId"></td>
            </tr>
            <tr>
                <td>비밀번호</td>
                <td><input type="password" name="userPw"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <button type="submit">로그인</button>
                    <button type="reset">취소</button>
                    <button type="button" onclick="location.href='join.php'">회원가입</button>
                </td>
            </tr>
        </table>
    </form>
</div>
<?php include_once '../include/footer.php' ?>

 

process/login_process.php

<?php
    session_start();
    // members 테이블에 등록된 회원인지 확인
    $conn = mysqli_connect('localhost', 'root', '0000', 'test');
    $query = "select * from members where id='{$_POST['userId']}'";
    $result = mysqli_query($conn, $query);

    // 아이디가 있다면 비밀번호 검사
    if(mysqli_num_rows($result)==1) {
        $row = mysqli_fetch_array($result);

        // 비밀번호 확인 -> 비밀번호가 맞으면 세션 생성
        if($_POST['userPw'] == $row['pw']) {
            $_SESSION['userId'] = $_POST['userId'];
            // 세션아이디가 있으면 로그인 되었습니다. 경고창 출력
            if(isset($_SESSION['userId'])) {
        ?>
        <script>
            alert("로그인 되었습니다.");
            location.replace("../index.php");
        </script>
        <?php        
            }
        } else {
        ?>
        <script>
            alert("비밀번호가 맞지 않습니다.");
            location.replace("../index.php");
        </script>    
        <?php
        }
    } else {
    ?>
    <script>
        alert("아이디가 맞지 않습니다.");
        location.replace("../index.php");
    </script>  
    <?php
    }
?>

 

process/logout_process.php

<?php
    session_start();
    $result = session_destroy();
    if($result) {
?>
<script>
    alert('로그아웃 되었습니다.');
    // location.replace('../index.php'); 
    history.back();   // 이전 페이지로 이동
</script>
<?php
    }
?>

 

 

 

search.php

<?php include_once 'include/header.php' ?>
<?php 
    function searchList() {
        // echo $_POST['search_m'];
        // echo $_POST['search'];
        $conn = mysqli_connect('localhost','root','0000','test');
        $query = "select * from books where `{$_POST['search_m']}` Like '%{$_POST['search']}%'";
        $result = mysqli_query($conn, $query);
        // echo '$result';
        // var_dump($result);
        //        if((int)$row[3] === (int)0) {
        //         echo "찾으시는 도서가 없습니다. 😅";
        //     } 
        while($row = mysqli_fetch_array($result)){
            echo "<tr>
                    <td>{$row['id']}</td>
                    <td><a href=\"detail.php?id={$row['id']}\">{$row['title']}</a></td>
                    <td>{$row['writer']}</td>
                    <td>{$row['publisher']}</td>
                    <td>{$row['price']}원</td>
                    <td>{$row['bookdate']}</td>
                </tr>";

        }

    }
?>
<div id="serch_book contents_page"  class="inner">
        <h2>검색한 도서목록입니다.</h2>
        <!-- <h3>총 --건 검색되었습니다.</h3> -->
        <table>
            <tr>
                <th>아이디</th>
                <th>제목</th>
                <th>글쓴이</th>
                <th>출판사</th>
                <th>가격</th>
                <th>출판일</th>
            </tr>
            <?php searchList(); ?>
        </table>
</div>
<?php include_once 'include/footer.php' ?>

 

 

 

css/style.css

@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+KR&display=swap');
* { margin: 0; padding: 0; box-sizing: border-box; }
li { list-style: none; }
a { color: inherit; text-decoration: none; }
:root {
    --main-color: #ef629f;
}
body {
    font-family: 'IBM Plex Sans KR', 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(120deg, #e0c3fc 0%, #8ec5fc 100%); */
    background-image: linear-gradient(to top, #a7a6cb 0%, #8989ba 52%, #8989ba 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;
    padding-left: 3px;
}
#searchDiv #searchBtn {
    background: var(--main-color);
    width: 100px;
    color: #fff;
    border: none;
    outline: none;
    text-align: center;
    line-height: 30px;
}
#searchDiv #rightBtn {
    display: inline;
    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: 10px;
}
#write_book table td:nth-child(1) {
    width: 30%;
}
#write_book table td:nth-child(2) {
    width: 70%;
    text-align: left;
}
#write_book input {
    width: 80%;
    line-height: 40px;
    padding-left: 10px;
    border: none;
    outline: none;
}
#write_book input, #write_book textarea {
    width: 80%;
    line-height: 30px;
    padding-left: 10px;
    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: 29px;
    border: none;
    outline: none;
}

#bestSeller_page ul {
    display: flex;
    flex-wrap: wrap;
}
#bestSeller_page ul li {
    width: 33.333%;
    padding: 80px 0;
    text-align: center;
}
#bestSeller_page ul li img {
    width: 70%;
}
#best_book table {
    border-collapse: collapse;
}
#best_book table td {
    border-bottom: 1px solid #fff;
    text-align: left;
    padding: 6px 16px;
}
#best_book table .tdcenter {
    text-align: center;
}
#best_book button {
    background: #fff;
    width: 120px;
    border-radius: 6px;
    border: none;
    outline: none;
    text-align: center;
    color: #222;
    line-height: 40px;
}

 


 

mysql의 books table