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
|
<?php session_start()?>
<!DOCTYPE html>
<html>
<head>
<title>Create a thread - cflip.net forum</title>
</head>
<body>
<?php include_once 'templates/header.php' ?>
<h2>Create a new thread</h2>
<?php
if (!isset($_SESSION['signed_in'])) {
die('You must be <a href="signin.php">signed in</a> to create a thread.');
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<label for="thread_subject">Subject: </label><br>
<input type="text" name="thread_subject"><br>
<label for="thread_cat">Category: </label><br>
<?php
include_once 'includes/db_inc.php';
include_once 'model/Category.php';
$categories = get_all_categories($dbc);
if (count($categories) == 0) {
echo 'There are no categories to post to!';
} else {
echo '<select name="thread_cat">';
foreach ($categories as $category) {
echo '<option value="' . $category->id . '">' . $category->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>
<?php
include_once 'includes/db_inc.php';
function insert_thread($dbc, $thread_subject, $thread_cat, $thread_author) {
$sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
$stmt = mysqli_stmt_init($dbc);
if (!mysqli_stmt_prepare($stmt, $sql)) {
die('Could not create thread due to internal error: ' . mysqli_error($dbc));
}
mysqli_stmt_bind_param($stmt, "sii", $thread_subject, $thread_cat, $thread_author);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
function insert_post($dbc, $post_content, $post_thread, $post_author) {
$sql = "INSERT INTO posts(post_content, post_date, post_thread, post_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
$stmt = mysqli_stmt_init($dbc);
if (!mysqli_stmt_prepare($stmt, $sql)) {
die('Could not create post due to internal error: ' . mysqli_error($dbc));
}
mysqli_stmt_bind_param($stmt, "sii", $post_content, $post_thread, $post_author);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING);
$thread_subject = filter_input(INPUT_POST, 'thread_subject', FILTER_SANITIZE_STRING);
$thread_cat = filter_input(INPUT_POST, 'thread_cat', FILTER_SANITIZE_NUMBER_INT);
$user_id = filter_var($_SESSION['user_id'], FILTER_SANITIZE_NUMBER_INT);
if (empty($thread_subject) or !$thread_subject) {
echo 'Thread subject cannot be empty';
} else {
insert_thread($dbc, $thread_subject, $thread_cat, $user_id);
$thread_id = mysqli_insert_id($dbc);
insert_post($dbc, $post_content, $thread_id, $user_id);
$sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1', `cat_post_count` = `cat_post_count` + '1' WHERE cat_id = " . $thread_cat . ";";
mysqli_query($dbc, $sql);
header("Location: /forum/thread/" . $thread_id);
}
}
?>
</body>
</html>
|