Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/projects/slutprojekt/
blala.php
chat.php
createtable.php
delete.php
deletetable.php
deletetables.php
fetch_messages.php
filhantering/
footer.php
header.php
home.php
login.php
new_password.php
password_reset.php
profile.php
send_message.php
signup.php
verify.php
delete.php
154 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
<?php
session_start();
if ($_SESSION['admin']!=1) {
header('Location: login.php');
}
include('../../dbconnection.php');
include ('header.php');
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<style>
body {
font-family: Arial, sans-serif;
padding: 2rem;
}
label, select, input {
margin-top: 1rem;
display: block;
}
.message {
margin-top: 1rem;
padding: 1rem;
background-color: #f1f1f1;
border-left: 4px solid #0077cc;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 2rem;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<?php
class DatabaseHandler {
private $pdo;
private $allowedTables = ['webusers', 'pictures', 'interests', 'friends', 'messages'];
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
public function getAllRows(string $table): array {
if (!in_array($table, $this->allowedTables)) {
return [];
}
try {
$stmt = $this->pdo->prepare("SELECT * FROM `$table`");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return [];
}
}
public function deleteById(string $table, int $id): string {
if (!in_array($table, $this->allowedTables)) {
return "Ogiltig tabell vald.";
}
try {
$stmt = $this->pdo->prepare("DELETE FROM `$table` WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
return "Raden med ID $id har tagits bort från '$table'.";
} else {
return "Ingen rad hittades med ID $id i '$table'.";
}
} catch (PDOException $e) {
return "Fel vid borttagning: " . $e->getMessage();
}
}
}
$meddelande = "";
$tabellData = [];
$selectedTable = isset($_POST['tabell']) ? $_POST['tabell'] : 'webusers'; // Bevara valt tabell
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$tabell = $_POST["tabell"] ?? 'webusers';
$id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
$db = new DatabaseHandler($dbconn);
if (isset($_POST['delete'])) {
$meddelande = $db->deleteById($tabell, $id);
}
$tabellData = $db->getAllRows($tabell);
}
?>
<h1>Ta bort rad från tabell</h1>
<?php if ($meddelande): ?>
<div class="message"><?php echo htmlspecialchars($meddelande); ?></div>
<?php endif; ?>
<form method="post">
<label for="tabell">Välj tabell:</label>
<select name="tabell" id="tabell" required onchange="this.form.submit()">
<option value="webusers" <?php echo ($selectedTable === 'webusers') ? 'selected' : ''; ?>>webusers</option>
<option value="pictures" <?php echo ($selectedTable === 'pictures') ? 'selected' : ''; ?>>pictures</option>
<option value="interests" <?php echo ($selectedTable === 'interests') ? 'selected' : ''; ?>>interests</option>
<option value="friends" <?php echo ($selectedTable === 'friends') ? 'selected' : ''; ?>>friends</option>
<option value="messages" <?php echo ($selectedTable === 'messages') ? 'selected' : ''; ?>>messages</option>
</select>
</form>
<?php if (count($tabellData) > 0): ?>
<h2>Rader i tabellen '<?php echo htmlspecialchars($selectedTable); ?>'</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Data</th>
<th>Åtgärd</th>
</tr>
</thead>
<tbody>
<?php foreach ($tabellData as $rad): ?>
<tr>
<td><?php echo htmlspecialchars($rad['id']); ?></td>
<td><?php echo htmlspecialchars(json_encode($rad)); ?></td>
<td>
<form method="post" style="display:inline;">
<input type="hidden" name="tabell" value="<?php echo htmlspecialchars($selectedTable); ?>">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($rad['id']); ?>">
<button type="submit" name="delete">Radera</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Inga rader att visa i tabellen '<?php echo htmlspecialchars($selectedTable); ?>'.</p>
<?php endif; ?>
</body>
</html>