-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_pharmacy_edit.php
107 lines (93 loc) · 4.27 KB
/
user_pharmacy_edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
$title = "薬局編集";
include 'header.php';
require_once 'functions.php';
if (!isset($_SESSION['user_id'])) {
header("Location: user_login.php");
exit;
}
$userId = $_SESSION['user_id'];
if (!isset($_GET['id'])) {
die("薬局IDが必要です。");
}
$db = connectDb();
$pharmacyId = $_GET['id'];
// 薬局情報を取得
$stmt = $db->prepare("SELECT * FROM Pharmacies WHERE id = ? AND owner_id = ?");
$stmt->execute([$pharmacyId, $userId]);
$pharmacy = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$pharmacy) {
die("薬局が見つかりません、またはアクセス権限がありません。");
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$metas = $_POST['meta'];
try {
$stmt = $db->prepare("UPDATE Pharmacies SET name = ?, address = ?, phone = ?, fax = ? WHERE id = ? AND owner_id = ?");
$stmt->execute([$name, $address, $phone, $fax, $pharmacyId, $userId]);
if(!empty($metas)){
foreach ($metas as $metakey => $value) {
$stmt = $db->prepare("SELECT COUNT(*) FROM PharmacyMeta WHERE pharmacy_id = ? AND metakey = ?");
$stmt->execute([$pharmacyId, $metakey]);
$exists = $stmt->fetchColumn();
if ($exists) {
$stmt = $db->prepare("UPDATE PharmacyMeta SET value = ? WHERE pharmacy_id = ? AND metakey = ?");
$stmt->execute([$value, $pharmacyId, $metakey]);
} else {
$stmt = $db->prepare("INSERT INTO PharmacyMeta (pharmacy_id, metakey, value) VALUES (?, ?, ?)");
$stmt->execute([$pharmacyId, $metakey, $value]);
}
}
}
setFlashMessage("薬局情報が更新されました。");
header("Location: user_profile.php");
exit;
} catch (PDOException $e) {
echo "エラー: " . $e->getMessage();
}
}
// Meta情報を取得
$stmt = $db->prepare("SELECT * FROM PharmacyMeta WHERE pharmacy_id = ?");
$stmt->execute([$pharmacyId]);
$metaData = $stmt->fetchAll(PDO::FETCH_ASSOC);
$metaDataAssoc = [];
foreach ($metaData as $meta) {
$metaDataAssoc[$meta['metakey']] = $meta['value'];
}
// MetaKeysを取得
$metaKeys = getMetaKeys();
?>
<div class="container mt-4">
<h1 class="mb-4">薬局編集</h1>
<form method="POST" action="">
<div class="mb-3">
<label for="name" class="form-label">薬局名</label>
<input type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($pharmacy['name'], ENT_QUOTES, 'UTF-8') ?>" required>
</div>
<div class="mb-3">
<label for="address" class="form-label">住所</label>
<input type="text" class="form-control" id="address" name="address" value="<?= htmlspecialchars($pharmacy['address'], ENT_QUOTES, 'UTF-8') ?>" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">電話番号</label>
<input type="text" class="form-control" id="phone" name="phone" value="<?= htmlspecialchars($pharmacy['phone'], ENT_QUOTES, 'UTF-8') ?>" required>
</div>
<div class="mb-3">
<label for="fax" class="form-label">FAX番号</label>
<input type="text" class="form-control" id="fax" name="fax" value="<?= htmlspecialchars($pharmacy['fax'], ENT_QUOTES, 'UTF-8') ?>" required>
</div>
<h2 class="mt-4">追加情報</h2>
<?php foreach ($metaKeys as $metaKey): ?>
<div class="mb-3">
<label for="meta_<?= htmlspecialchars($metaKey['metakey'], ENT_QUOTES, 'UTF-8') ?>" class="form-label"><?= htmlspecialchars($metaKey['description'], ENT_QUOTES, 'UTF-8') ?></label>
<input type="text" class="form-control" id="meta_<?= htmlspecialchars($metaKey['metakey'], ENT_QUOTES, 'UTF-8') ?>" name="meta[<?= htmlspecialchars($metaKey['metakey'], ENT_QUOTES, 'UTF-8') ?>]" value="<?= isset($metaDataAssoc[$metaKey['metakey']]) ? htmlspecialchars($metaDataAssoc[$metaKey['metakey']], ENT_QUOTES, 'UTF-8') : '' ?>">
</div>
<?php endforeach; ?>
<button type="submit" class="btn btn-primary">更新</button>
</form>
</div>
</body>
</html>