Files
PHP-EM-DB/public/product_update.php
2026-10-29 08:36:44 +09:00

310 lines
9.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/../database/products/ProductAccess.php';
/**
* 商品更新処理(Update)
*
* このファイルでは、既存の商品情報を更新します。
*/
// エラー表示設定(開発時のみ)
ini_set('display_errors', 1);
error_reporting(E_ALL);
$message = '';
$messageType = '';
$product = null;
// POSTリクエストの処理(更新実行)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update'])) {
try {
$id = (int)$_POST['id'];
// TODO: ProductAccessのインスタンスを作成してください
$productAccess = null; // ここを実装
// TODO: POSTデータから商品情報を配列にまとめてください
$productData = [
// ここにPOSTデータを設定
];
// TODO: productAccessのupdate()メソッドを呼び出して商品を更新してください
$message = "商品情報を更新しました!";
$messageType = "success";
} catch (Exception $e) {
// TODO: エラーメッセージを$messageに設定してください
$message = "";
$messageType = "error";
}
}
// GETリクエストの処理(商品情報取得)
if (isset($_GET['id'])) {
try {
$id = (int)$_GET['id'];
// TODO: ProductAccessのインスタンスを作成してください
$productAccess = null; // ここを実装
// TODO: productAccessのread()メソッドを呼び出して商品情報を取得してください
$product = null; // ここを実装
if (empty($product)) {
$message = "指定された商品が見つかりませんでした。";
$messageType = "error";
}
} catch (Exception $e) {
$message = "商品の取得に失敗しました: " . $e->getMessage();
$messageType = "error";
}
}
// 全商品一覧を取得(セレクトボックス用)
$products = [];
$categories = [];
try {
$productAccess = new ProductAccess();
$products = $productAccess->read();
$categories = $productAccess->getCategories();
} catch (Exception $e) {
$message = "データの取得に失敗しました: " . $e->getMessage();
$messageType = "error";
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商品更新 - CRUD操作</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
color: #666;
margin-bottom: 25px;
font-size: 14px;
}
.nav-links {
display: flex;
gap: 10px;
margin-bottom: 25px;
flex-wrap: wrap;
}
.nav-links a {
padding: 8px 16px;
background: #667eea;
color: white;
text-decoration: none;
border-radius: 5px;
font-size: 14px;
transition: background 0.3s;
}
.nav-links a:hover {
background: #5568d3;
}
.message {
padding: 15px;
margin-bottom: 20px;
border-radius: 5px;
font-weight: bold;
}
.message.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.message.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.section {
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
color: #333;
font-weight: bold;
}
input[type="text"],
input[type="number"],
select,
textarea {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 14px;
transition: border-color 0.3s;
}
input[type="text"]:focus,
input[type="number"]:focus,
select:focus,
textarea:focus {
outline: none;
border-color: #667eea;
}
textarea {
resize: vertical;
min-height: 100px;
}
.btn {
padding: 12px 24px;
background: #667eea;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: background 0.3s;
}
.btn:hover {
background: #5568d3;
}
.btn-search {
width: 100%;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>✏️ 商品更新</h1>
<p class="subtitle">Update - 既存の商品情報を更新します</p>
<div class="nav-links">
<a href="index.php">🏠 トップ</a>
<a href="product_list.php">📋 一覧表示</a>
<a href="product_register.php">📝 新規登録</a>
<a href="product_delete.php">🗑️ 削除</a>
<a href="product_join.php">🔗 JOIN検索</a>
</div>
<?php if ($message): ?>
<div class="message <?php echo $messageType; ?>">
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<!-- 商品選択セクション -->
<div class="section">
<h2>1. 更新する商品を選択</h2>
<form method="GET" action="">
<div class="form-group">
<label for="product_select">商品選択</label>
<select name="id" id="product_select">
<option value="">選択してください</option>
<?php foreach ($products as $p): ?>
<option value="<?php echo $p['id']; ?>"
<?php echo (isset($_GET['id']) && $_GET['id'] == $p['id']) ? 'selected' : ''; ?>>
[ID: <?php echo $p['id']; ?>] <?php echo htmlspecialchars($p['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-search">商品情報を取得</button>
</form>
</div>
<!-- 更新フォームセクション -->
<?php if ($product): ?>
<div class="section">
<h2>2. 商品情報を更新</h2>
<form method="POST" action="">
<input type="hidden" name="id" value="<?php echo $product['id']; ?>">
<div class="form-group">
<label for="category_id">カテゴリ *</label>
<select name="category_id" id="category_id" required>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category['id']; ?>"
<?php echo ($product['category_id'] == $category['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($category['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="name">商品名 *</label>
<input type="text" name="name" id="name"
value="<?php echo htmlspecialchars($product['name']); ?>" required>
</div>
<div class="form-group">
<label for="description">説明</label>
<textarea name="description" id="description"><?php echo htmlspecialchars($product['description'] ?? ''); ?></textarea>
</div>
<div class="form-group">
<label for="price">価格 *</label>
<input type="number" name="price" id="price" step="0.01" min="0"
value="<?php echo $product['price']; ?>" required>
</div>
<div class="form-group">
<label for="stock">在庫数 *</label>
<input type="number" name="stock" id="stock" min="0"
value="<?php echo $product['stock']; ?>" required>
</div>
<button type="submit" name="update" class="btn">更新する</button>
</form>
</div>
<?php endif; ?>
</div>
</body>
</html>