一覧へ戻る

select_update.php

<?php
require_once __DIR__ . '/dbconnect.php';

$sql = 'SELECT id, name, size, memo FROM animal ORDER BY id';
$stmt = $dbh->query($sql);
$animals = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>更新データ選択 PDO版</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container my-4">
    <h1>更新データ選択</h1>
    <p><a href="index.html">トップページへ戻る</a></p>
    <table class="table table-bordered table-striped align-middle">
        <thead>
        <tr><th>id</th><th>name</th><th>size</th><th>memo</th><th>操作</th></tr>
        </thead>
        <tbody>
        <?php foreach ($animals as $animal): ?>
            <tr>
                <form method="post" action="update.php">
                    <td>
                        <?= h((string)$animal['id']) ?>
                        <input type="hidden" name="id" value="<?= h((string)$animal['id']) ?>">
                    </td>
                    <td><input type="text" name="name" class="form-control" value="<?= h($animal['name']) ?>" required></td>
                    <td><input type="number" name="size" class="form-control" value="<?= h((string)$animal['size']) ?>" required></td>
                    <td><input type="text" name="memo" class="form-control" value="<?= h($animal['memo']) ?>"></td>
                    <td><button type="submit" class="btn btn-primary btn-sm">更新</button></td>
                </form>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
</body>
</html>