-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.php
102 lines (78 loc) · 2.94 KB
/
activity.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
<?php
// include configuration file
include('config.php');
// connect to the database
$db = mysqli_connect ($db_host, $db_user, $db_password, $db_name) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
// continue session
session_start();
// check for a user_id
if(!$_SESSION['user_id'])
{
// redirect user to homepage if they are not signed in, change path if necessary
header("Location: /");
}
?>
<!-- HTML -->
<!-- top navigation -->
<?php include('header.php'); ?>
<!-- content -->
<div class="container" style="margin-top: 65px">
<h2 class="text-primary">Hi <?php echo "{$_SESSION['nickname']}"; ?>!</h2>
<?php
// check for shout removal
if($_GET['action'] == 'remove')
{
$sql = "SELECT user_id FROM shouts2 WHERE shout_id = '{$_GET['id']}' LIMIT 1";
$result = mysqli_query($db, $sql) or die('Query failed: ' . mysqli_error($db));
$row = mysqli_fetch_assoc($result);
// check ownership
if($row['user_id'] == $_SESSION['user_id'])
{
// delete shout
$sql = "DELETE FROM shouts2 WHERE shout_id = '{$_GET['id']}' LIMIT 1";
$result = mysqli_query($db, $sql) or die('Query failed: ' . mysqli_error($db));
// display confirmation, change path if necessary
echo "<div class=\"alert alert-success\">Nachricht wurde erfolgreich gelöscht!<a href=\"activity.php\" class=\"alert-link pull-right close\">×</a></div>";
}
}
// check for shout submission
if(isset($_POST['submit']))
{
// empty error array
$error = array();
// check for a shout
if(empty($_POST['shout']))
{
$error[] = 'Beitrag fehlt';
}
// if there are no errors, insert shout into the database.
// otherwise, display errors.
if(sizeof($error) == 0)
{
// insert shout
$sql = "INSERT INTO shouts2 (shout_id, user_id, shout, shout_date) VALUES (null, '{$_SESSION['user_id']}', '{$_POST['shout']}', NOW())";
$result = mysqli_query($db, $sql) or die('Query failed: ' . mysqli_error($db));
// display confirmation
echo "<div class=\"alert alert-success alert-dismissible\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Schliessen\"><span aria-hidden=\"true\">×</span></button>Nachricht wurde erfolgreich hinzugefügt!</div>";
} else {
// display error message
foreach($error as $value)
{
echo "<div class=\"text-error\">{$value}</div>";
}
}
}
?>
<!-- shoutbox form -->
<form method="post" action="activity.php" style="margin-bottom: 25px">
<div class="form-group">
<textarea name="shout" placeholder="Was willst du schreiben?" class="form-control" rows="5"></textarea>
</div>
<input name="submit" type="submit" value="Senden" class="btn btn-primary" />
</form>
<!-- show messages -->
<div class="messages">
<?php include('messages.php'); ?>
</div>
</body>
</html>