-
Notifications
You must be signed in to change notification settings - Fork 0
/
createtopic.php
185 lines (159 loc) · 4.81 KB
/
createtopic.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
<?php
session_start();
include 'connect.php';
include 'header.php';
echo '<h2>Create a Topic to start discussion</h2>';
if(!isset($_SESSION['login_user']))
{
echo 'Please <a href="loginpage.php">sign in</a> to continue and create a topic';
}
else
{
?>
<!DOCTYPE html>
<html>
<head>
<style>
textarea {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 10px;
box-sizing: border-box;
}
input[type=text], select {
width: 100%;
padding: 12px 30px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 30px;
box-sizing: border-box;
}
input[type=submit] {
width: 100%;
background-color: #47c9af;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color:#16e5bb;
}
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
<?php
if($_SERVER['REQUEST_METHOD']!='POST')
{
$sql= "SELECT cat_id,cat_name,cat_description FROM categories";
$result=mysql_query($sql);
if(!$result)
{
echo 'Something went wrong please try again';
}
else
{
if(mysql_num_rows($result)==0)
{
if($SESSION['user_level']==1)
{
echo 'You have not created any categories';
}
else
{
echo 'You are not permitted to create any categories';
}
}
else
{
echo '<div>';
echo '<form method="post" action="">
Subject: <input type="text" id="fname" name="topic_subject" required />
Category:';
echo '<select name="topic_cat" id="country">';
while($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
}
echo '</select>';
echo 'Message: <textarea name="post_content" required /></textarea>
<input type="submit" value="Create topic" />
</form>';
echo '</div>';
}
}
}
else
{
$query = "BEGIN WORK;";
$result = mysql_query($query);
if(!$result)
{
echo 'An error occured while creating your topic. Please try again later.';
}
else
{
if(!empty($_SESSION['login_user']))
{
$sql = "INSERT INTO
topics(topic_subject,
topic__date,
topic_category,
topic_byuser)
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
NOW(),
" . mysql_real_escape_string($_POST['topic_cat']) . ",
" . $_SESSION['userid'] . "
)";
$result = mysql_query($sql);
if(!$result)
{
echo 'An error occured while inserting your data. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
$result = mysql_query($sql);
}
else
{
$topicid = mysql_insert_id();
$sql = "INSERT INTO
posts(post_content,
post_time,
post_topic,
post_by)
VALUES
('" . mysql_real_escape_string($_POST['post_content']) . "',
NOW(),
" . $topicid . ",
" . $_SESSION['userid'] . "
)";
$result = mysql_query($sql);
if(!$result)
{
echo 'An error occured while inserting your post. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
$result = mysql_query($sql);
}
else
{
$sql = "COMMIT;";
$result = mysql_query($sql);
echo 'You have successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
}
}
}
else
echo"Error";
}
}
}
include 'footer.php';
?>