Category : PHP

PHPのCRUD(Create / Read / Update / Delete)は、データベース操作の基本構造

2026年06月15
10View

header.php

共通テンプレート(header部分)

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="ja">

<head>
    <title>Photo Gallery</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>

    <!-- ヘッター -->
    <nav class="navbar navbar-dark bg-dark p-3">
        <span class="navbar-brand mb-0 h1">Gallery</span>
    </nav>
    <div class="container mt-5">
        <h2>画像ギャラリー</h2>
        <div class="row g-4">
            <?php

if (isset($_SESSION['error'])) {
    echo '<div class="alert alert-danger" role="alert">' . $_SESSION['error'] . '</div>';
    unset($_SESSION['error']);
}
if (isset($_SESSION['success'])) {
    echo '<div class="alert alert-success" role="alert">' . $_SESSION['success'] . '</div>';
    unset($_SESSION['success']);
}
?>

            <?php
include 'dbh.php';
$sql = "SELECT * FROM photos ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) { ?>
            <div class="col-sm-6 col-md-3">
                <div class="card">
                    <img src="uploaded_files/<?php echo $row['filename']; ?>" width="300" height="225"
                        class="card-img-top" alt="<?php echo $row['title']; ?>">
                    <div class="card-body">
                        <h3 class="card-text"><?php echo $row['title']; ?></h3>
                        <p class="card-text border-top pt-2"><?php echo $row['description']; ?></p>
                    </div>
                </div>
            </div>

            <?php }
} else {
    echo "No photos found.";
}
$conn->close();
?>


        </div>


        <div class=" d-flex justify-content-center mt-5">
            <form action="upload.php" method="post" enctype="multipart/form-data" class="">
                <h2 class="text-center">UPLOAD</h2>

                <!-- ファイル名 -->
                <div class="mb-3">
                    <label for="filename" class="form-label">ファイル名</label>
                    <input type="text" class="form-control rounded-pill" id="filename" name="filename"
                        placeholder="ファイル名を入力">
                </div>

                <!-- タイトル -->
                <div class="mb-3">
                    <label for="title" class="form-label">タイトル</label>
                    <input type="text" class="form-control rounded-pill" id="title" name="title" placeholder="タイトルを入力">
                </div>

                <!-- 説明 -->
                <div class="mb-3">
                    <label for="description" class="form-label">説明</label>
                    <textarea class="form-control rounded-3" id="description" name="description" rows="2"
                        style="resize: none;" placeholder="説明を入力"></textarea>
                </div>

                <!-- 写真ファイル -->
                <div class="mb-3">
                    <label for="photo" class="form-label">写真</label>
                    <input type="file" class="form-control rounded-pill" id="photo" name="photo" accept="image/*">
                </div>

                <!-- 送信ボタン -->
                <button type="submit" class="btn btn-primary rounded-pill" name="save_photo">
                    アップロード
                </button>

            </form>

        </div>
    </div>
    <div class="mt-5 p-4 bg-dark text-white text-center">
        <p>Footer</p>
    </div>
</body>
</html>

Theme Customization

Layout:
Vertical
Horizontal
Semi Box
Content Width:
Default
Box
Layout Direction:
LTR
RTL
Layout Mode:
Light
Dark
System
Sidebar Size:
Default
Medium
Icon
Icon hover
Sidebar Color:
Light
Dark