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
|
<?php
include 'header.php';
include_once 'connect.php';
echo '<section><h2>Create a new topic</h2>';
if (!isset($_SESSION['signed_in'])) {
echo 'You must be <a href="signin.php">signed in</a> to create a topic.';
} else {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$sql = "SELECT cat_id, cat_name, cat_description FROM categories";
$result = mysqli_query($dbc, $sql);
if (!$result) {
echo 'Error while selecting from database. Please try again later.';
} else {
if (mysqli_num_rows($result) == 0) {
echo 'There are currently no categories to post to.';
} else {
echo '
<form action="" method="post">
<label for="topic_subject">Subject: </label><br>
<input type="text" name="topic_subject"><br>
<label for="topic_cat">Category: </label><br>
<select name="topic_cat">';
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
}
echo '
</select><br>
<label for="post_content">Write your post: </label><br>
<textarea name="post_content"></textarea><br>
<input type="submit" name="submit">
</form>
';
}
}
} else {
$sql = "BEGIN WORK;";
$result = mysqli_query($dbc, $sql);
if (!$result) {
echo 'An error occurred creating your topic. Try again later';
} else {
$sql = "INSERT INTO topics(topic_subject, topic_date, topic_cat, topic_author) VALUES(
'" . mysqli_real_escape_string($dbc, $_POST['topic_subject']) . "',
NOW(),
" . mysqli_real_escape_string($dbc, $_POST['topic_cat']) . ",
" . $_SESSION['user_id'] .")";
$result = mysqli_query($dbc, $sql);
if (!$result) {
echo 'An error occured while creating your post. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
mysqli_query($dbc, $sql);
} else {
$topic_id = mysqli_insert_id($dbc);
$sql = "INSERT INTO posts(post_content, post_date, post_topic, post_author) VALUES(
'" . mysqli_real_escape_string($dbc, $_POST['post_content']) . "',
NOW(),
" . $topic_id . ",
" . $_SESSION['user_id'] . ")";
$result = mysqli_query($dbc, $sql);
if (!$result) {
echo 'An error occured while creating your post. Please try again later.' . mysqli_error($dbc);
$sql = "ROLLBACK;";
mysqli_query($dbc, $sql);
} else {
$sql = "COMMIT;";
$result = mysqli_query($dbc, $sql);
echo 'You have successfully created <a href="topic.php?id='. $topic_id . '">your new topic</a>.';
}
}
}
}
}
echo '</section>';
include 'footer.php';
?>
|