-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUA_MySQL.php
458 lines (392 loc) · 18.1 KB
/
SUA_MySQL.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
// https://www.php-kurs.com/loesung-einlogg-script.htm
// session_start();
// $_SESSION['last_visit'] = time();
// $session_timeout = 1800;
// if((time() - $_SESSION['last_visit']) > $session_timeout) {session_destroy();}
// if ( isset($_GET['logout'])){ session_destroy(); header('Location: index.php'); exit;}
// if ( isset($_POST['benutzername']) and $_POST['benutzername'] != "" and isset($_POST['kennwort']) and $_POST['kennwort'] != "" )
// {if ($_POST['benutzername'] == "admin" AND $_POST['kennwort'] == "admin" or $_POST['benutzername'] == "finanzen" AND $_POST['kennwort'] == "finanzen" or $_POST['benutzername'] == "verwaltung" AND $_POST['kennwort'] == "verwaltung" )
// {$_SESSION['benutzername'] = $_POST['benutzername']; $_SESSION['eingeloggt'] = true;} //echo "<b>einloggen erfolgreich</b>";}
// else{echo "<b>ungültige Eingabe</b>"; $_SESSION['eingeloggt'] = false;}}
// if ( isset($_SESSION['eingeloggt']) and $_SESSION['eingeloggt'] == true ); //{echo "<h1>Hallo ". $_SESSION['benutzername'] . "</h1>";}
// else{echo '<form action="'. $_SERVER['SCRIPT_NAME'] .'" method="POST"><p>Benutzername:<br><input type="text" name="benutzername" value=""><p>Kennwort:<br><input type="password" name="kennwort" value=""><p><input type="Submit" value="einloggen"></form>';exit;}
$app_name = 'Simple User Administration';
// aktuelle Zeit für das "geaendert" FIELD
$dt = new DateTime("now", new DateTimeZone('Europe/Berlin'));
$local_time = $dt->format('d.m.Y H:i');
$local_time_sort = $dt->format('Y_m_d_H_i_');
// make connection
$tablename = 'user';
$db = new PDO('mysql:host=localhost;dbname=d032596b;charset=utf8', 'd032596b', 'TkHvthJ7qRsUr25z' );
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//////////////////// JUST FOR THE FIRST RUN
$sql= "CREATE TABLE IF NOT EXISTS $tablename(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
erstellt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
geaendert TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Name VARCHAR(30) NOT NULL)";
$db->exec($sql);
// first data
try{
$db->exec("ALTER TABLE $tablename ADD COLUMN Nachname VARCHAR(30) NOT NULL");
$db->exec("ALTER TABLE $tablename ADD COLUMN WG VARCHAR(30) NOT NULL");
$db->exec("ALTER TABLE $tablename ADD COLUMN eingezogen VARCHAR(30) NOT NULL");
$db->exec("ALTER TABLE $tablename ADD COLUMN ausgezogen VARCHAR(30) NOT NULL ");
$db->exec("ALTER TABLE $tablename ADD COLUMN Telefon VARCHAR(30) NOT NULL ");
$db->exec("ALTER TABLE $tablename ADD COLUMN Email VARCHAR(30) NOT NULL ");
$db->exec("ALTER TABLE $tablename ADD COLUMN Freifeld_P VARCHAR(3500) NOT NULL ");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Paul', 'Panther', 'EG links', '18.03.2005', '', '0800 2566 5214', '[email protected]', 'Text und so...')");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Elke', 'Eichhörnchen', 'EG rechts', '15.11.2014', '', '0800 4579 2548', 'elke@eichhörnchen.de', 'Text und so...')");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Heinz', 'Hummel', '1 OG links', '19.01.2014', '11.03.2020', '0800 2566 9547', '[email protected]', 'Text und so...')");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Karin', 'Känguru', '2 OG mitte', '25.09.2008', '', '0800 5863 2478', '[email protected]', 'Text und so...')");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Moritz', 'Marder', '2 OG rechts', '11.03.2020', '11.03.2020', '0800 8956 2478', '[email protected]', 'Text und so...')");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Anne', 'Aal', '1 OG mitte', '16.04.2017', '', '0800 8569 8569', '[email protected]', 'Text und so...')");
$db-> exec("INSERT INTO $tablename(Name, Nachname, WG, eingezogen, ausgezogen, Telefon, Email ,Freifeld_P )VALUES('Ute', 'Unke', '2 OG mitte', '10.06.2017', '', '0800 7425 8547', '[email protected]', 'Text und so...')");
} catch (Exception $e) {}
//////////////////// JUST FOR THE FIRST RUN
if( isset($_GET['id']) ){$id = $_GET['id'];}
if( isset($_POST['id']) ){$id = $_POST['id'];}
// // // // // // // delete user // // // // // // //
if (isset($_GET['delete'])) {
$id = $_GET['id'];
$query = "DELETE FROM $tablename WHERE id=$id";
$db->exec($query);
$titel = "gelöscht";
}
// // // // // // // create a new user // // // // // // //
if (isset($_POST['new_user'])) {
foreach ($_POST as $param_name => $param_val) {
if ($param_name != 'new_user' && $param_name != 'id') {
$name[] = $param_name;
$value[] = $param_val;
}
}
$db->exec("INSERT INTO $tablename (" . implode(",", $name) . ") VALUES ('" . implode("','", $value) . "')");
// get the id of the new user to show him
$stmt = $db->prepare("SELECT MAX(id) AS max_id FROM $tablename");
$stmt -> execute();
$invNum = $stmt -> fetch(PDO::FETCH_ASSOC);
$id = $invNum['max_id'];
$titel = $_POST['Name']."angelegt";
}
// // // // // // // update the user data // // // // // // //
if (isset($_POST['update_user'])) {
$id = $_POST['id'];
//echo "ID: ".array_values($row)[$id]['id'];
//print_r($_POST);
foreach ($_POST as $param_name => $param_val) {
if ($param_name != 'update_user' && $param_name != 'id' ) {
$query = "UPDATE $tablename set $param_name ='$param_val' WHERE id=$id ";
$db->exec($query);
}
}
}
// // // // // // // get the data for user // // // // // // //
if(!isset($_GET['new']) AND isset($_GET['user'])){
$result = $db->query("SELECT * FROM $tablename WHERE id = $id");
while( $row = $result->fetch(PDO::FETCH_ASSOC) ) {
$rows = $row;
}
// print_r($rows);
}
// // // // // // // get the data for index // // // // // // //
if(!isset($_GET['user'])){
// show only this columns in the index 'erstellt', 'geaendert',
$input2=['id', 'Name', 'Nachname', 'WG', 'eingezogen', 'ausgezogen', 'Telefon', 'Email'];
$query = "SELECT ".implode(', ', $input2)." FROM $tablename";
$sth = $db->query($query);
while( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$rows[] = $row; // appends each row to the array
}
if (isset($_GET['sort'])) {$sort = $_GET['sort'];} else {$sort = 'id';}
array_multisort( array_column($rows, $sort), SORT_ASC, $rows );
//print_r($rows);
$fieldnames = array_keys($rows[0]);
}
?>
<!-- // // // // // // HTML & CSS // // // // // // // // // // -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $titel; ?></title>
<style>
/* table > tbody > tr > td{border: 1px solid red; border-collapse: collapse;}table > tbody > tr > td > table > tbody > tr > td{border: 1px solid blue; border-collapse: collapse;}table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr > td{border: 1px solid green; border-collapse: collapse;} */
body{color:#333436; }
a{text-decoration: none;}
a.blue, a.blue:visited{color: #2d399a;}
a.blue:hover{color: #1b225c;}
a.green, a.green:visited{color: #3a9a2d;}
a.green:hover{color: #173e12;}
a.red, a.red:visited{color: #9a2d3a;}
a.red:hover{color: #3e1217;}
td>h1{display: inline;font-size: 2em;font-weight: bold; }
td>h2{display: inline;font-size: 1.5em;font-weight: bold; }
td>h3{display: inline;font-size: 1.2em;font-weight: bold; }
td>h3>a{color:#333436;}
table, tr, td{border-style:none; border-bottom:0px solid #eee; border-collapse: collapse;}
tr.pointer{cursor:pointer;}
tr.hover{transition: 0.3s;}
tr.hover:hover:nth-child(5n+1) {background: #9fa6e3}
tr.hover:hover:nth-child(5n+2) {background: #9fe3cd}
tr.hover:hover:nth-child(5n+3) {background: #e3b29f}
tr.hover:hover:nth-child(5n+4) {background: #c2e39f}
tr.hover:hover:nth-child(5n+5) {background: #e39fdd}
td.logo{width:310px; height:110px;}
td.headline{padding-left: 5px;padding-right: 25px;}
td.user_cell{padding: 5px;}
td{padding: 1px;}
td{vertical-align: top; text-align: left;}
td.header{width: 220px;}
td.inter_heading{vertical-align: bottom; height:33px;}
td.freifeld{display: block; width:400px; height: 200px; overflow-y: scroll}
td.key{height:25px; width: 100px;}
td.value{width: 200px;}
td.links{text-align: right; width: 180px;}
td.small{font-size: 70%;}
input[type=text], input[type=password], input[type=email], input[type=number]{border: 1px solid grey;}
textarea{border-style: none;}
</style>
<script type="text/javascript"> function tr_click(url){document.location.href = url; }</script>
</head>
<?php
// // // // // // // HTML for index // // // // // // //
if(!isset($_GET['user'])){
?>
<body>
<!-- PAGE -->
<table>
<tr>
<td colspan="8">
<!-- HEADER -->
<table>
<tr>
<td class="logo"><a href="/burgis/index.php"><img class="logo" src="files/Stina_2_100.png"></a></td>
<td class="header">
<!-- DATENSATZ -->
<table>
<tr><td colspan="2"><h1><? echo $app_name; ?></h1></td></tr>
<!-- <tr><td class="small"> </td><td class="small"> </td></tr> -->
<!-- <tr><td class="small"> </td><td class="small"> </td></tr> -->
<tr><td class="small"> </td><td class="small"> </td></tr>
</table>
<!-- DATENSATZ -->
</td>
<td class="header">
<!-- HYPERLINKS -->
<table>
<tr><td class="links"><a class="green" href="index.php?user&new">Person anlegen</a></td></tr>
<tr><td class="links"> </td></tr>
<tr><td class="links"> </td></tr>
<tr><td class="links"><a class="red" href="index.php?logout">abmelden</a></td></tr>
</table>
<!-- HYPERLINKS -->
</td>
</tr>
</table>
<!-- HEADER -->
</td>
</tr>
<tr>
<td>
<table>
<tr>
<?
// Schleife macht aus den $fieldnames der DB Überschriften für die Tabelle
echo "\t";
foreach ($fieldnames AS $field){
echo "<td class=\"headline\"><h3><a href=\"index.php?sort=".$field."\">".$field."</a></h3></td>\n\t\t\t";
}
echo "</tr>\n\t\t";
// Schleift durch das multi Array, erst jeder User eine Reihe, dann jeder Item eine Splate
foreach ($rows as $key_row=>$val_row) {
echo "\t<tr onclick=\"tr_click('index.php?user&id=" . $val_row['id'] . "')\" class=\"hover pointer\" >\n";
echo "\t\t\t<td class=\"user_cell\"><a class=\"blue\" href=\"index.php?user&id=" . $val_row['id'] . "\">" . $val_row['id'] . "</a></td>\n";
foreach ($val_row as $key_field=>$val_field) {
if ($key_field == 'id') continue;
echo "\t\t\t<td class=\"user_cell\" >" . $val_field . "</td>\n";
}
echo "\t</tr>\n";
} ?>
</table>
</body>
</html>
<?php
}
// // // // // // // HTML for index // // // // // // //
?>
<?php if(isset($_GET['user'])){
// case allocation
if(isset($_GET['update'])){
$id = $rows['id'];
$textarea_css = "style=\"border: 1px solid grey; \"";
$form_action = "index.php?user&id=".$rows['id'];
$update_link = " ";
$delete_link = " ";
// PERSON
$erstellt = $rows['erstellt'];
$geaendert = $rows['geaendert'];
$name = "<input type=\"text\" name=\"Name\" size=\"8\" value=\"".$rows['Name']."\">";
$name_header = "<input name=\"update_user\" type=\"submit\" form=\"formular\" value=\"Änderungen für ".$rows['Name']." speichern\"> ";
$nachname = "<input type=\"text\" name=\"Nachname\" size=\"8\" value=\"".$rows['Nachname']."\">";
$wg = "<input type=\"text\" name=\"WG\" size=\"21\" value=\"".$rows['WG']."\">";
$eingezogen = "<input type=\"text\" name=\"eingezogen\" size=\"21\" value=\"".$rows['eingezogen']."\">";
$ausgezogen = "<input type=\"text\" name=\"ausgezogen\" size=\"21\" value=\"".$rows['ausgezogen']."\">";
$telefon = "<input type=\"text\" name=\"Telefon\" size=\"21\" value=\"".$rows['Telefon']."\">";
$email = "<input type=\"text\" name=\"Email\" size=\"21\" value=\"".$rows['Email']."\">";
$freifeld_p = "<textarea name=\"Freifeld_P\" cols=\"45\" rows=\"30\">".$rows['Freifeld_P']."</textarea>";
// PERSON
}
elseif(isset($_GET['new'])){
$id = $rows['id'];
$textarea_css = "style=\"border: 1px solid grey; \"";
$form_action = "index.php";
$update_link = " ";
$delete_link = " ";
// PERSON
$erstellt = "<input type=\"text\" name=\"erstellt\" size=\"18\" readonly value=\"".$local_time."\">";
$geaendert = "<input type=\"text\" name=\"geaendert\" size=\"18\" readonly value=\"".$local_time."\">";
$name = "<input type=\"text\" name=\"Name\" size=\"8\" >";
$name_header = "<input name=\"new_user\" type=\"submit\" form=\"formular\" value=\"neue Person anlegen\"> ";
$nachname = "<input type=\"text\" name=\"Nachname\" size=\"8\" value=\"".$rows['Nachname']."\">";
$wg = "<input type=\"text\" name=\"WG\" size=\"21\" value=\"".$rows['WG']."\">";
$eingezogen = "<input type=\"text\" name=\"eingezogen\" size=\"21\" value=\"".$rows['eingezogen']."\">";
$ausgezogen = "<input type=\"text\" name=\"ausgezogen\" size=\"21\" value=\"".$rows['ausgezogen']."\">";
$telefon = "<input type=\"text\" name=\"Telefon\" size=\"21\" value=\"".$rows['Telefon']."\">";
$email = "<input type=\"text\" name=\"Email\" size=\"21\" value=\"".$rows['Email']."\">";
$freifeld_p = "<textarea name=\"Freifeld_P\" cols=\"45\" rows=\"30\">".$rows['Freifeld_P']."</textarea>";
// PERSON
} else {
$id = $rows['id'];
$textarea_css = "style=\"white-space: pre-line\"";
$form_action = "index.php?user&id=".$rows['id'];
$update_link = "<a class=\"blue\" href=\"index.php?user&update&id=".$id."\">bearbeiten</a>";
$delete_link = "<a class=\"red\" href=\"index.php?delete&id=".$id."\" onclick=\"return confirm('Soll ".strtoupper($rows['Name'] ." ". $rows['Nachname'])." wirklich gelöscht werden?');\">löschen</a>";
// PERSON
$erstellt = $rows['erstellt'];
$geaendert = $rows['geaendert'];
$name = $rows['Name'];
$name_header = $rows['Name'];
$nachname = $rows['Nachname'];
$wg = $rows['WG'];
$eingezogen = $rows['eingezogen'];
$ausgezogen = $rows['ausgezogen'];
$telefon = $rows['Telefon'];
$email = $rows['Email'];
$freifeld_p = $rows['Freifeld_P'];
// PERSON
}
?>
<body>
<!-- PAGE -->
<table>
<tr>
<td>
<!-- HEADER -->
<table>
<tr>
<td class="logo"><a href="/burgis/index.php"><img class="logo" src="files/Stina_2_100.png"></a></td>
<td class="header">
<!-- DATENSATZ -->
<table>
<tr><td colspan="2"><h1><? echo $name_header; ?></h1></td></tr>
<tr><td class="small"> </td><td class="small"> </td></tr>
<tr><td class="small">erstellt: </td><td class="small"> <? echo $erstellt; ?></td></tr>
<tr><td class="small">geändert: </td><td class="small"> <? echo $geaendert; ?></td></tr>
</table>
<!-- DATENSATZ -->
</td>
<td class="header">
<!-- HYPERLINKS -->
<table>
<tr><td class="links"><a class="green" href="index.php">Übersicht</a></td></tr>
<tr><td class="links"><a class="red" href="index.php?logout">abmelden</a></td></tr>
<tr><td class="links"><? echo $update_link ?></td></tr>
<tr><td class="links"><? echo $delete_link; ?></td></tr>
</table>
<!-- HYPERLINKS -->
</td>
</tr>
</table>
<!-- HEADER -->
</td>
</tr>
<form action="<?php echo $form_action; ?>" method="post" id="formular">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="hidden" name="geaendert" value="<?php echo $local_time; ?>">
<!-- BLOCK PERSON -->
<tr>
<td>
<table>
<tr>
<td>
<!-- LEFT TABLE -->
<table>
<tr><td class="inter_heading" colspan="2"><h2>Person</h2></td></tr>
<tr><td class="key">Name: </td><td class="value"><? echo $name; ?> <? echo $nachname; ?></td></tr>
<tr><td class="key">eingezogen: </td><td class="value"><? echo $eingezogen; ?></td></tr>
<tr><td class="key">Wohnung: </td><td class="value"><? echo $wg; ?></td></tr>
<tr><td class="key">ausgezogen: </td><td class="value"><? echo $ausgezogen; ?></td></tr>
<tr><td class="key">Telefon: </td><td class="value"><? echo $telefon; ?></td></tr>
<tr><td class="key">E-Mail: </td><td class="value"><? echo $email; ?></td></tr>
</table>
<!-- LEFT TABLE -->
</td>
<td>
<!-- RIGHT FIELD -->
<table>
<tr><td class="inter_heading"><h3>Freifeld Person</h3></td></tr>
<tr><td class="freifeld" <? echo $textarea_css ?> > <? echo $freifeld_p ?></td></tr>
</table>
<!-- RIGHT FIELD -->
</td>
</tr>
</table>
</td>
</tr>
<!-- BLOCK PERSON -->
<!-- BLOCK FINANZEN -->
<tr>
<td>
<table>
<tr>
<td>
<!-- LEFT TABLE -->
<table>
<tr><td class="inter_heading" colspan="2"><h2>Finanzen</h2></td></tr>
</table>
<!-- LEFT TABLE -->
</td>
<td>
<!-- RIGHT FIELD -->
<table>
<tr><td class="inter_heading"><h3>Freifeld Finanzen</h3></td></tr>
</table>
<!-- RIGHT FIELD -->
</td>
</tr>
</table>
</td>
</tr>
<!-- BLOCK FINANZEN -->
</form>
</table>
<!-- PAGE -->
</body>
</html>
<?php } // index.php?user ?>
<?php
//
// function get_val($arr,$nr1, $nr2){
// return current(array_slice($arr, $nr, 1));
// }
//
// foreach ($rows as $key=>$val) {
// echo $val['id'][0];
// echo " <br>";
// foreach ($val as $key=>$val) {
// echo $val;
// }
// }
?>