Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/projects/slutprojekt/
class/
create-categories.php
create-recipe.php
css/
db_content.php
forgot_password.php
include/
login.php
logout.php
recipe-search.php
recipe.php
reset_password.php
signin.php
start.php
tabeller/
verify.php
db_content.php
215 lines UTF-8 Windows (CRLF)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
session_start();
include('../../dbconnection.php');
ob_clean();
include('include/session-variables.php');
if($_SESSION['admin'] != true) {
header("Location: login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/styles.css">
<title>Hantera Data</title>
<style>
.container {
padding: 20px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.table-container {
overflow-x: auto;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.data-table {
width: 100%;
border-collapse: collapse;
}
.data-table th, .data-table td {
padding: 8px 12px;
border: 1px solid #ddd;
text-align: left;
}
.data-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.data-table tbody tr:nth-child(even) {
background-color: #f9f9f9;
}
.delete-button {
background-color: #f44336;
color: white;
border: none;
padding: 6px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
border-radius: 4px;
cursor: pointer;
}
.delete-button:hover {
background-color: #d32f2f;
}
.success-message {
color: green;
margin-bottom: 10px;
padding: 10px;
border: 1px solid green;
background-color: #e6ffe6;
border-radius: 5px;
}
.error-message {
color: red;
margin-bottom: 10px;
padding: 10px;
border: 1px solid red;
background-color: #ffe6e6;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="page-container">
<div id="content-wrap">
<?php include('include/header.php'); ?>
<div class="container">
<h1>Hantera Data</h1>
<?php
if (isset($_GET['delete_table']) && isset($_GET)) {
$tableName = $_GET['delete_table'];
$idToDelete = null;
$primaryKeyColumn = '';
if ($tableName === 'users' && isset($_GET['user_id'])) {
$primaryKeyColumn = 'user_id';
$idToDelete = $_GET['user_id'];
} elseif ($tableName === 'categories' && isset($_GET['category_id'])) {
$primaryKeyColumn = 'category_id';
$idToDelete = $_GET['category_id'];
} elseif ($tableName === 'recipes' && isset($_GET['recipe_id'])) {
$primaryKeyColumn = 'recipe_id';
$idToDelete = $_GET['recipe_id'];
} elseif ($tableName === 'recipe_categories' && isset($_GET['recipe_id'])) {
$primaryKeyColumn = 'recipe_id';
$idToDelete = $_GET['recipe_id'];
$categoryToDelete = $_GET['category_id'] ?? null;
} elseif ($tableName === 'comments' && isset($_GET['comment_id'])) {
$primaryKeyColumn = 'comment_id';
$idToDelete = $_GET['comment_id'];
} elseif ($tableName === 'ratings' && isset($_GET['rating_id'])) {
$primaryKeyColumn = 'rating_id';
$idToDelete = $_GET['rating_id'];
} elseif ($tableName === 'favorites' && isset($_GET['favorite_id'])) {
$primaryKeyColumn = 'favorite_id';
$idToDelete = $_GET['favorite_id'];
}
if ($primaryKeyColumn && $idToDelete !== null) {
try {
$sql = "DELETE FROM $tableName WHERE $primaryKeyColumn = :id";
if ($tableName === 'recipe_categories' && isset($categoryToDelete)) {
$sql .= " AND category_id = :category_id";
}
$stmt = $dbconn->prepare($sql);
$stmt->bindParam(':id', $idToDelete);
if ($tableName === 'recipe_categories' && isset($categoryToDelete)) {
$stmt->bindParam(':category_id', $categoryToDelete);
}
$stmt->execute();
if ($stmt->rowCount() > 0) {
echo "<div class='success-message'>Raden med ID $idToDelete i tabellen $tableName har tagits bort.</div>";
} else {
echo "<div class='error-message'>Kunde inte ta bort raden med ID $idToDelete i tabellen $tableName.</div>";
}
} catch (PDOException $e) {
echo "<div class='error-message'>Fel vid borttagning från $tableName: " . htmlspecialchars($e->getMessage()) . "</div>";
}
} else {
echo "<div class='error-message'>Ogiltiga parametrar för borttagning.</div>";
}
}
displayTable($dbconn, 'users');
displayTable($dbconn, 'categories');
displayTable($dbconn, 'recipes');
displayTable($dbconn, 'recipe_categories');
displayTable($dbconn, 'comments');
displayTable($dbconn, 'ratings');
displayTable($dbconn, 'favorites');
function displayTable($dbconn, $tableName) {
try {
$sql = "SELECT * FROM $tableName";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($results) {
echo "<div class='table-container'>";
echo "<h2>Tabell: $tableName</h2>";
echo "<table class='data-table'>";
echo "<thead><tr>";
foreach ($results[0] as $column => $value) {
echo "<th>" . htmlspecialchars($column) . "</th>";
}
echo "<th>Åtgärder</th>";
echo "</tr></thead><tbody>";
foreach ($results as $row) {
echo "<tr>";
foreach ($row as $column => $value) {
echo "<td>" . htmlspecialchars($value) . "</td>";
}
$primaryKeyColumn = '';
if ($tableName === 'users') $primaryKeyColumn = 'user_id';
elseif ($tableName === 'categories') $primaryKeyColumn = 'category_id';
elseif ($tableName === 'recipes') $primaryKeyColumn = 'recipe_id';
elseif ($tableName === 'recipe_categories') $primaryKeyColumn = 'recipe_id';
elseif ($tableName === 'comments') $primaryKeyColumn = 'comment_id';
elseif ($tableName === 'ratings') $primaryKeyColumn = 'rating_id';
elseif ($tableName === 'favorites') $primaryKeyColumn = 'favorite_id';
if ($primaryKeyColumn) {
$deleteUrl = "?delete_table=$tableName&$primaryKeyColumn=" . htmlspecialchars($row[$primaryKeyColumn]);
if ($tableName === 'recipe_categories' && isset($row['category_id'])) {
$deleteUrl .= "&category_id=" . htmlspecialchars($row['category_id']);
}
echo "<td><a class='delete-button' href='$deleteUrl' onclick='return confirm(\"Är du säker på att du vill ta bort denna rad?\");'>Ta bort</a></td>";
} else {
echo "<td>Ingen primärnyckel</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
echo "</div><br>";
} else {
echo "<p>Ingen data hittades i tabellen $tableName.</p><br>";
}
} catch (PDOException $e) {
echo "<div class='error-message'>Fel vid hämtning av data från $tableName: " . htmlspecialchars($e->getMessage()) . "</div><br>";
}
}
?>
</div>
<?php include('include/footer.php'); ?>
</div>
</div>
</body>
</html>