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

337 lines
9.9 KiB
PHP
Raw 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';
/**
* 商品削除処理(Delete)
*
* このファイルでは、既存の商品をデータベースから削除します。
*/
// エラー表示設定(開発時のみ)
ini_set('display_errors', 1);
error_reporting(E_ALL);
$message = '';
$messageType = '';
$product = null;
// POSTリクエストの処理(削除実行)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete'])) {
try {
$id = (int)$_POST['id'];
// TODO: ProductAccessのインスタンスを作成してください
$productAccess = null; // ここを実装
// TODO: productAccessのdelete()メソッドを呼び出して商品を削除してください
$message = "商品を削除しました!";
$messageType = "success";
$product = null; // 削除後は商品情報をクリア
} catch (Exception $e) {
// TODO: エラーメッセージを$messageに設定してください
$message = "";
$messageType = "error";
}
}
// GETリクエストの処理(商品情報取得)
if (isset($_GET['id']) && empty($_POST)) {
try {
$id = (int)$_GET['id'];
$productAccess = new ProductAccess();
$product = $productAccess->read($id);
if (empty($product)) {
$message = "指定された商品が見つかりませんでした。";
$messageType = "error";
}
} catch (Exception $e) {
$message = "商品の取得に失敗しました: " . $e->getMessage();
$messageType = "error";
}
}
// 全商品一覧を取得(セレクトボックス用)
$products = [];
$categories = [];
try {
$productAccess = new ProductAccess();
$products = $productAccess->read();
$categoriesData = $productAccess->getCategories();
foreach ($categoriesData as $cat) {
$categories[$cat['id']] = $cat['name'];
}
} 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;
}
select {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 14px;
transition: border-color 0.3s;
}
select:focus {
outline: none;
border-color: #667eea;
}
.btn {
padding: 12px 24px;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: background 0.3s;
}
.btn-search {
background: #667eea;
width: 100%;
margin-top: 10px;
}
.btn-search:hover {
background: #5568d3;
}
.btn-danger {
background: #e74c3c;
}
.btn-danger:hover {
background: #c0392b;
}
.product-info {
background: #f8f9fa;
padding: 20px;
border-radius: 5px;
margin-bottom: 20px;
}
.product-info h3 {
margin-bottom: 15px;
color: #e74c3c;
}
.info-row {
display: flex;
padding: 10px 0;
border-bottom: 1px solid #ddd;
}
.info-label {
font-weight: bold;
width: 120px;
color: #555;
}
.info-value {
flex: 1;
color: #333;
}
.warning {
background: #fff3cd;
border: 1px solid #ffc107;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
color: #856404;
}
</style>
</head>
<body>
<div class="container">
<h1>🗑️ 商品削除</h1>
<p class="subtitle">Delete - 既存の商品を削除します</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_update.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>
<div class="warning">
⚠️ この操作は取り消せません。本当に削除してよろしいですか?
</div>
<div class="product-info">
<h3>削除対象の商品情報</h3>
<div class="info-row">
<span class="info-label">商品ID:</span>
<span class="info-value"><?php echo htmlspecialchars($product['id']); ?></span>
</div>
<div class="info-row">
<span class="info-label">カテゴリ:</span>
<span class="info-value"><?php echo htmlspecialchars($categories[$product['category_id']] ?? '不明'); ?></span>
</div>
<div class="info-row">
<span class="info-label">商品名:</span>
<span class="info-value"><?php echo htmlspecialchars($product['name']); ?></span>
</div>
<div class="info-row">
<span class="info-label">説明:</span>
<span class="info-value"><?php echo htmlspecialchars($product['description'] ?? '-'); ?></span>
</div>
<div class="info-row">
<span class="info-label">価格:</span>
<span class="info-value">¥<?php echo number_format($product['price']); ?></span>
</div>
<div class="info-row">
<span class="info-label">在庫数:</span>
<span class="info-value"><?php echo htmlspecialchars($product['stock']); ?></span>
</div>
</div>
<form method="POST" action="" onsubmit="return confirm('本当に削除しますか?');">
<input type="hidden" name="id" value="<?php echo $product['id']; ?>">
<button type="submit" name="delete" class="btn btn-danger">削除する</button>
</form>
</div>
<?php endif; ?>
</div>
</body>
</html>