{PHP} 예약 프로그램 만들기 - 개발 ( my page., log out)

minseo·2024년 2월 23일
post-thumbnail

목차

  • MY PAGE - 정보 수정
    - HTML
    - PHP
    - CSS
  • MY PAGE - 예약 확인
    - HTML
    - PHP
    - CSS
  • LOG OUT
    - PHP

MY PAGE - 정보 수정

HTML

Mypage.php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Page</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="MyPage.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Libre+Bodoni&family=Noto+Sans+KR:wght@100;300&display=swap"
        rel="stylesheet">
</head>

<body>
    <?php
    include('./header.php');
    ?>
    <div class="navbar">
        <div class="nav3">
            <ul>
                <li><a href="MyPage.php">내 정보 변경</a></li>
                <li><a href="Mypage_reser.php">예약 확인</a></li>
            </ul>
        </div>
        <div class="container">
            <div class="login-form">
                <form action="" method="post">
                    <?php
                    include('./db_conn.php');

                    // 현재 로그인한 사용자의 ID 가져오기
                    $userid = isset($_COOKIE["userid"]) ? $_COOKIE["userid"] : null;
                    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                        // POST로 전송된 데이터 가져오기
                        $uemail = mysqli_real_escape_string($conn, $_POST["uemail"]);
                        $umobile = mysqli_real_escape_string($conn, $_POST["umobile"]);

                        // 사용자 정보 업데이트 쿼리
                        $update_query = "UPDATE member SET mail='$uemail', phone='$umobile' WHERE user_id='$userid'";

                        if (mysqli_query($conn, $update_query)) {
                            echo '<script>alert("정보가 성공적으로 업데이트되었습니다!");</script>';
                        } else {
                            echo '<script>alert("업데이트 실패 !");</script>';
                        }
                    }
                    // 사용자 정보 가져오기
                    $query = "SELECT * FROM member  WHERE user_id='$userid'";
                    $result = mysqli_query($conn, $query);

                    if (!$result) {
                        die("Query failed: " . mysqli_error($conn));
                    }

                    // 결과 출력
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo '<form action="" method="post">';
                        echo '    <input type="text" name="id" class="text-field" placeholder="아이디" value="' . $row['user_id'] . '" readonly><br>';
                        echo '    <input type="text" name="password" class="text-field" placeholder="이름" value="' . $row['username'] . '" readonly><br>';
                        echo '    <input type="email" name="uemail" class="text-field" placeholder="e-mail" value="' . $row['mail'] . '"><br>';
                        echo '    <input type="text" name="umobile" class="text-field" id="mobile" placeholder="전화번호" value="' . $row['phone'] . '"><br>';
                        echo '    <input type="submit" value="수정하기" class="submit-btn">';
                        echo '</form>';
                    }
                    // 결과 해제
                    mysqli_free_result($result);

                    // 데이터베이스 연결 종료
                    $conn->close();
                    ?>

                </form>
            </div>
        </div>
    </div>
</body>

</html>

PHP

<?php
                    include('./db_conn.php');

                    // 현재 로그인한 사용자의 ID 가져오기
                    $userid = isset($_COOKIE["userid"]) ? $_COOKIE["userid"] : null;
                    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                        // POST로 전송된 데이터 가져오기
                        $uemail = mysqli_real_escape_string($conn, $_POST["uemail"]);
                        $umobile = mysqli_real_escape_string($conn, $_POST["umobile"]);

                        // 사용자 정보 업데이트 쿼리
                        $update_query = "UPDATE member SET mail='$uemail', phone='$umobile' WHERE user_id='$userid'";

                        if (mysqli_query($conn, $update_query)) {
                            echo '<script>alert("정보가 성공적으로 업데이트되었습니다!");</script>';
                        } else {
                            echo '<script>alert("업데이트 실패 !");</script>';
                        }
                    }
                    // 사용자 정보 가져오기
                    $query = "SELECT * FROM member  WHERE user_id='$userid'";
                    $result = mysqli_query($conn, $query);

                    if (!$result) {
                        die("Query failed: " . mysqli_error($conn));
                    }

                    // 결과 출력
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo '<form action="" method="post">';
                        echo '    <input type="text" name="id" class="text-field" placeholder="아이디" value="' . $row['user_id'] . '" readonly><br>';
                        echo '    <input type="text" name="password" class="text-field" placeholder="이름" value="' . $row['username'] . '" readonly><br>';
                        echo '    <input type="email" name="uemail" class="text-field" placeholder="e-mail" value="' . $row['mail'] . '"><br>';
                        echo '    <input type="text" name="umobile" class="text-field" id="mobile" placeholder="전화번호" value="' . $row['phone'] . '"><br>';
                        echo '    <input type="submit" value="수정하기" class="submit-btn">';
                        echo '</form>';
                    }
                    // 결과 해제
                    mysqli_free_result($result);

                    // 데이터베이스 연결 종료
                    $conn->close();
                    ?>

html코드 안에 php 코드를 작성해 주었다.
먼저 이미 작성되어있던 정보를 출력 -> 입력 받기 -> 수정 이런식으로 하는 방식이다.

CSS

Mypage.css

.navbar{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    height: 100vh;
    margin: 30px;
}
.nav3 ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.nav3 li {
    
    display: inline;
    margin-right: 20px; /* Adjust the margin as needed */
}

.nav3 a {
    font-size: 25px;
    text-decoration: none;
    color: #333; /* Set the desired text color */
    font-weight: bold; /* Set the desired font weight */
}
.login-form {
    width: 400px;
    background-color: #EEEFF1;
    margin-right: auto;
    margin-left: auto;
    margin-top: 50px;
    padding: 20px;
    text-align: center;
    border: none;
    border-radius: 5px;
    box-sizing: border-box; /* login-form에는 border-box 적용 */
    font-family: 'Noto Sans KR', sans-serif;
}

.text-field {
    font-family: 'Noto Sans KR', sans-serif;
    font-size: 14px;
    padding: 10px;
    border: none;
    width: 260px;
    margin-bottom: 10px;
    border-radius: 5px;
    box-sizing: border-box;
}

.submit-btn {
    font-family: 'Noto Sans KR', sans-serif;
    border-radius: 5px;
    box-sizing: border-box;
    font-size: 14px;
    border: none;
    padding: 10px;
    width: 260px;
    background-color: #791b1b;
    margin-bottom: 30px;
    color: white;
}

MY PAGE - 예약 확인

HTML

Mypage_reser.php

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register Page</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="reser.css">
    <link rel="stylesheet" href="MyPage.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Libre+Bodoni&family=Noto+Sans+KR:wght@100;300&display=swap"
        rel="stylesheet">
</head>

<body>
    <?php
    include('./header.php');
    ?>
    <div class="navbar">
        <div class="nav3">
            <ul>
                <li><a href="MyPage.php">내 정보 변경</a></li>
                <li><a href="mypage_reser.php">예약 확인</a></li>

            </ul>
        </div>
        <table>
            <thead>
                <tr>
                    <th>예약 날짜</th>
                    <th>인원</th>
                </tr>
            </thead>
            <tbody>
                <?php
                include('./db_conn.php');
                $query = "SELECT reser_date, person FROM reservation WHERE users_id='$user_id'";
                $result = mysqli_query($conn, $query);

                if (!$result) {
                    die("Query failed: " . mysqli_error($conn));
                }

                while ($row = mysqli_fetch_assoc($result)) {
                    echo
                        '<tr>'
                        . '<td>' . $row['reser_date'] . '</td>'
                        . '<td>' . $row['person'] . '</td>'
                        . '</tr>';
                }

                mysqli_free_result($result);

                // 데이터베이스 연결 종료
                mysqli_close($conn);
                ?>
            </tbody>
        </table>
    </div>

</body>

</html>

PHP

<?php
                include('./db_conn.php');
                $query = "SELECT reser_date, person FROM reservation WHERE users_id='$user_id'";
                $result = mysqli_query($conn, $query);

                if (!$result) {
                    die("Query failed: " . mysqli_error($conn));
                }

                while ($row = mysqli_fetch_assoc($result)) {
                    echo
                        '<tr>'
                        . '<td>' . $row['reser_date'] . '</td>'
                        . '<td>' . $row['person'] . '</td>'
                        . '</tr>';
                }

                mysqli_free_result($result);

                // 데이터베이스 연결 종료
                mysqli_close($conn);
                ?>

정보 수정과 똑같이 html 안에 작성해주었다.

CSS

reser.css

body {
    font-family: Consolas, monospace;
    font-family: 12px;
  }
  table {
    width: 100%;
  }
  th, td {
    text-align: center;
    padding: 10px;
    border-bottom: 1px solid #dadada;
  }

LOG OUT

PHP

로그 아웃은 다른 창이 뜨는 것이 아니라 , 클릭을 하면 쿠키가 삭제 되는 것 이기 때문에 php 코드만 있다.
logout.php

<?php
// 세션 시작
session_start();

// 모든 세션 변수 제거
$_SESSION = array();

// 세션 파기
session_destroy();

// 사용자 쿠키를 삭제하기 위해 만료일을 과거로 설정
setcookie("userid", "", time() - 3600, "/");

// 로그아웃 후 메인 페이지 또는 다른 페이지로 리디렉션
header("Location: main.php");
exit();
?>

쿠키가 삭제 되고 메인 화면으로 돌아간다.

0개의 댓글