-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.php
145 lines (117 loc) · 5.27 KB
/
post.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
<?php
session_start();
include('connection.php');
if (isset($_POST['submit'])) {
// Validate form data
function validateFormData($formData) {
$formdata = trim(stripslashes(htmlspecialchars($formData)));
return $formData;
}
// Set all variables empty by default
$content = "";
if (!$_POST['content']) {
$contentError = "Please enter some content <br>";
}
else {
$content = validateFormData($_POST['content']);
}
$user = $_SESSION['loggedInUser'];
if ($user && $content) {
$post_id = $_SESSION['postID'];
$query = "INSERT INTO comments (post_id, comment_content, comment_user, comment_date) VALUES ('$post_id', '$content', '$user', CURRENT_TIMESTAMP)";
if (mysqli_query($conn, $query)) {
echo "<div class='alert alert-success'>New record in database!</div>";
header("Location: post.php" . "?id=" . $_SESSION['postID']);
}
else {
echo "Error: " . $query ."<br>" . mysqli_error($conn);
}
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Post Page</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">
<title>my forum</title>
<!-- Custom CSS -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container col-sm-6 col-sm-offset-3">
<div class="navbar">
<nav>
<ul class="nav nav-pills pull-right">
<li><a href="index.php">Home</a></li>
<?php if($_SESSION['loggedInUser']) { ?>
<li><a href="logout.php">Logout</a></li>
<?php } else { ?>
<li><a href="login.php">Login</a></li>
<li><a href="register.php">Register</a></li>
<?php } ?>
</ul>
</nav>
<h3 class="text-muted"><a href="/">My Forum</a></h3>
</div>
<?php
if (isset($_GET['id'])) {
include('connection.php');
$id = $_GET['id'];
$query = "SELECT * FROM posts WHERE id=$id";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<h3>" . $row['titel'] . "</h3>";
echo "<p>" . $row['content'] . "</p>";
echo "<div class='clearfix'><small class='pull-right'>Posted by " . $row['user'] . " on " . $row['post_date'] . "</small></div>";
echo "<hr>";
}
}
}
?>
<?php
if (isset($_GET['id'])) {
include('connection.php');
$id = $_GET['id'];
$comments = "SELECT comment_content, comment_user FROM comments WHERE post_id=$id";
$commentresults = mysqli_query($conn, $comments);
if (mysqli_num_rows($commentresults) > 0) {
while ($commentrow = mysqli_fetch_assoc($commentresults)) {
echo "<div class='row'>";
echo "<div class='col-sm-2'>" . $commentrow['comment_user'] . "</div>";
echo "<div class='col-sm-10'>" . $commentrow['comment_content'] . "</div>";
echo "</div>";
echo "<hr>";
}
}
}
?>
<!-- if logged in there should be a form to create post -->
<?php
if ($_SESSION['loggedInUser']) {
$_SESSION['postID'] = $_GET['id'];
?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<legend>Reply to post</legend>
<div class="form-group">
<small class="text-danger"><?php echo $contentError; ?></small>
<textarea class="form-control" rows="6" placeholder="Comment content" name="content"></textarea>
</div>
<input type="submit" class="btn btn-default" name="submit" value="Comment">
</form>
<?php } ?>
</div>
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<!-- Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>