Repository navigation
Expand file tree
/
Copy pathteas.php
More file actions
188 lines (173 loc) · 7.32 KB
/
Copy pathteas.php
File metadata and controls
188 lines (173 loc) · 7.32 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
require_once 'includes/database.php';
require_once 'includes/header.php';
require_once 'includes/functions.php';
$editingId = isset($_GET['edit']) ? (int) $_GET['edit'] : null;
$showAddForm = isset($_GET['add']);
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_tea'])) {
$date = trim($_POST['date'] ?? '');
$brand = trim($_POST['brand'] ?? '');
$name = trim($_POST['name'] ?? '');
$flavor = trim($_POST['flavor'] ?? '');
$errors = validateTeaInput($date, $brand, $name);
if (empty($errors)) {
$stmt = $mysqli->prepare("INSERT INTO teas (drink_date, brand, tea_name, flavor) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $date, $brand, $name, $flavor);
if ($stmt->execute()) {
$message = showMessage("Tea added!", 'success');
$showAddForm = false;
} else {
$message = showMessage("Error adding tea.", 'error');
}
} else {
$message = showMessage(implode(' ', $errors), 'error');
$showAddForm = true;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
$id = (int) $_POST['id'];
$date = trim($_POST['date'] ?? '');
$brand = trim($_POST['brand'] ?? '');
$name = trim($_POST['name'] ?? '');
$flavor = trim($_POST['flavor'] ?? '');
$errors = validateTeaInput($date, $brand, $name);
if (empty($errors)) {
$mysqli->begin_transaction();
try {
$stmt = $mysqli->prepare("UPDATE teas SET drink_date=?, brand=?, tea_name=?, flavor=? WHERE id=?");
$stmt->bind_param("ssssi", $date, $brand, $name, $flavor, $id);
$stmt->execute();
$action = $mysqli->real_escape_string("Updated tea #$id");
$mysqli->query("INSERT INTO tea_audit (tea_id, action) VALUES ($id, '$action')");
$mysqli->commit();
$message = showMessage("Tea updated!", 'success');
$editingId = null;
} catch (Exception $e) {
$mysqli->rollback();
$message = showMessage("Error: " . $e->getMessage(), 'error');
}
} else {
$message = showMessage(implode(' ', $errors), 'error');
$editingId = $id ?? null;
}
}
if (isset($_GET['delete'])) {
$del_id = (int) $_GET['delete'];
$stmt = $mysqli->prepare("DELETE FROM teas WHERE id = ?");
$stmt->bind_param("i", $del_id);
$stmt->execute();
$message = showMessage("Tea deleted!", 'success');
}
function validateTeaInput($date, $brand, $name)
{
$errors = [];
if (!validateDateFilter($date)) {
$errors[] = "Invalid date format. Use YYYY-MM-DD.";
}
if (empty($brand)) {
$errors[] = "Brand is required.";
} else {
$brand_clean = str_replace([' ', '-', "'", '.'], '', $brand);
if (!ctype_alpha($brand_clean)) {
$errors[] = "Brand should contain only letters, spaces, hyphens, apostrophes, and dots.";
}
}
if (empty($name)) {
$errors[] = "Tea name is required.";
}
return $errors;
}
$total_teas = getTeaCount($mysqli);
$result = $mysqli->query("SELECT * FROM teas ORDER BY drink_date DESC");
$total = $result->num_rows;
?>
<h2>All Teas</h2>
<p>Total teas: <?php echo $total; ?></p>
<?php echo $message; ?>
<div class="teas-actions">
<?php if (!$showAddForm): ?>
<a href="teas.php?add=1" class="btn-add">+ Add Tea</a>
<?php endif; ?>
</div>
<?php if ($showAddForm): ?>
<div class="add-form-box">
<h3>Add New Tea</h3>
<form method="POST">
<div class="form-row">
<label>Date</label>
<input type="date" name="date" value="<?php echo date('Y-m-d'); ?>" required>
</div>
<div class="form-row">
<label>Brand</label>
<input type="text" name="brand" placeholder="e.g. Fructus" required>
</div>
<div class="form-row">
<label>Name</label>
<input type="text" name="name" placeholder="e.g. Earl Grey" required>
</div>
<div class="form-row">
<label>Flavor</label>
<input type="text" name="flavor" placeholder="e.g. Bergamot">
</div>
<div class="form-row form-btns">
<button type="submit" name="add_tea">Save</button>
<a href="teas.php"><button type="button" class="btn-cancel">Cancel</button></a>
</div>
</form>
</div>
<?php endif; ?>
<?php if ($total > 0): ?>
<div class="table-scroll">
<table>
<tr>
<th>ID</th>
<th>Date</th>
<th>Brand</th>
<th>Name</th>
<th>Flavor</th>
<th>Actions</th>
</tr>
<?php while ($tea = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $tea['id']; ?></td>
<?php if ($editingId == $tea['id']): ?>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $tea['id']; ?>">
<td><input type="date" name="date" value="<?php echo $tea['drink_date']; ?>"></td>
<td><input type="text" name="brand" value="<?php echo htmlspecialchars($tea['brand']); ?>"></td>
<td><input type="text" name="name" value="<?php echo htmlspecialchars($tea['tea_name']); ?>"></td>
<td><input type="text" name="flavor" value="<?php echo htmlspecialchars($tea['flavor']); ?>"></td>
<td>
<button type="submit" name="save">Save</button>
<a href="teas.php"><button type="button" class="btn-cancel">Cancel</button></a>
</td>
</form>
<?php else: ?>
<td><?php echo htmlspecialchars($tea['drink_date']); ?></td>
<td><?php echo htmlspecialchars($tea['brand']); ?></td>
<td><?php echo htmlspecialchars($tea['tea_name']); ?></td>
<td><?php echo htmlspecialchars($tea['flavor']); ?></td>
<td>
<span class="ier-buttons">
<form method="GET" style="display:inline">
<input type="hidden" name="edit" value="<?php echo $tea['id']; ?>">
<button type="submit">Edit</button>
</form>
</span>
<span class="ier-buttons">
<form method="GET" style="display:inline" onsubmit="return confirm('Delete this tea?')">
<input type="hidden" name="delete" value="<?php echo $tea['id']; ?>">
<button type="submit" class="btn-danger">Delete</button>
</form>
</span>
</td>
<?php endif; ?>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php else: ?>
<?php echo showMessage("No teas found. Import some data or add one above!", 'warning'); ?>
<?php endif; ?>
<?php require_once 'includes/footer.php'; ?>