diff options
author | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-23 12:25:51 -0700 |
---|---|---|
committer | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-23 12:25:51 -0700 |
commit | c23ad7cbf42f9590b4e0418b77c333f9dc7730e7 (patch) | |
tree | c3db1eef2d3db416579259f36dcdb3d2aef5ac8e | |
parent | 0b26a9cd485d5b1ed509d9da998780d8b658eb8a (diff) |
Change terminology from topics to threads
-rw-r--r-- | category.php | 10 | ||||
-rw-r--r-- | create_topic.php | 99 | ||||
-rw-r--r-- | header.php | 2 | ||||
-rw-r--r-- | includes/db_inc.php | 2 | ||||
-rw-r--r-- | includes/register_inc.php | 0 | ||||
-rw-r--r-- | includes/reply_inc.php | 6 | ||||
-rw-r--r-- | index.php | 4 | ||||
-rw-r--r-- | setup.sql | 22 | ||||
-rw-r--r-- | topic.php | 67 |
9 files changed, 23 insertions, 189 deletions
diff --git a/category.php b/category.php index b444ef3..fcc7851 100644 --- a/category.php +++ b/category.php @@ -27,21 +27,21 @@ mysqli_free_result($result); echo '</section>'; -$sql = "SELECT topic_id, topic_subject, topic_date, user_name FROM topics LEFT JOIN users ON topic_author = user_id WHERE topic_cat = " . mysqli_real_escape_string($dbc, $_GET['id']) . " ORDER BY topic_id DESC"; +$sql = "SELECT thread_id, thread_subject, thread_date, user_name FROM threads LEFT JOIN users ON thread_author = user_id WHERE thread_cat = " . mysqli_real_escape_string($dbc, $_GET['id']) . " ORDER BY thread_id DESC"; $result = mysqli_query($dbc, $sql); if (!$result) { - die('Error trying to display topics: ' . mysqli_error($dbc)); + die('Error trying to display threads: ' . mysqli_error($dbc)); } // Display table of posts -echo '<table><tr><th class="left">Topic</th><th class="right">Latest Post</th></tr>'; +echo '<table><tr><th class="left">Thread</th><th class="right">Latest Post</th></tr>'; while ($row = mysqli_fetch_assoc($result)) { echo '<tr><td class="left">'; - echo '<h4><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a></h4>'; - echo '<small>by <b>' . $row['user_name'] . '</b> on ' . date('M d, Y', strtotime($row['topic_date'])) . '</small></td><td class="right">by <b>cflip</b><br><small>01-22-2021 9:34</small></td></tr>'; + echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>'; + echo '<small>by <b>' . $row['user_name'] . '</b> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small></td><td class="right">by <b>cflip</b><br><small>01-22-2021 9:34</small></td></tr>'; } echo '</table>'; diff --git a/create_topic.php b/create_topic.php deleted file mode 100644 index 278d0fa..0000000 --- a/create_topic.php +++ /dev/null @@ -1,99 +0,0 @@ -<?php - -include_once 'header.php'; - -echo '<section><h2>Create a new topic</h2>'; - -if (!isset($_SESSION['signed_in'])) { - die('You must be <a href="signin.php">signed in</a> to create a topic.'); -} -?> - -<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> - <label for="topic_subject">Subject: </label><br> - <input type="text" name="topic_subject"><br> - <label for="topic_cat">Category: </label><br> - <?php - include_once 'includes/db_inc.php'; - - $sql = "SELECT cat_id, cat_name, cat_description FROM categories"; - $result = mysqli_query($dbc, $sql); - - if (!$result) { - die('Error trying to fetch category list: ' . mysqli_error($dbc)); - } - - if (mysqli_num_rows($result) == 0) { - die('There are currently no categories to post to.'); - } - - echo '<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> -</section> - -<?php -include_once 'includes/db_inc.php'; - -function create_topic($dbc, $topic_subject, $topic_cat, $topic_author) { - $sql = "INSERT INTO topics(topic_subject, topic_date, topic_cat, topic_author) VALUES(?, NOW(), ?, ?);"; - $stmt = mysqli_stmt_init($dbc); - - if (!mysqli_stmt_prepare($stmt, $sql)) { - die('Could not create topic due to internal error: ' . mysqli_error($dbc)); - } - - mysqli_stmt_bind_param($stmt, "sii", $topic_subject, $topic_cat, $topic_author); - mysqli_stmt_execute($stmt); - mysqli_stmt_close($stmt); -} - -function create_post($dbc, $post_content, $post_topic, $post_author) { - $sql = "INSERT INTO posts(post_content, post_date, post_topic, post_author) VALUES(?, NOW(), ?, ?);"; - $stmt = mysqli_stmt_init($dbc); - - if (!mysqli_stmt_prepare($stmt, $sql)) { - die('Could not create topic due to internal error: ' . mysqli_error($dbc)); - } - - mysqli_stmt_bind_param($stmt, "sii", $post_content, $post_topic, $post_author); - mysqli_stmt_execute($stmt); - mysqli_stmt_close($stmt); -} - -function validate($data) { - $data = trim($data); - $data = stripslashes($data); - $data = htmlspecialchars($data); - return $data; -} - -if ($_SERVER['REQUEST_METHOD'] == 'POST') { - $post_content = validate($_POST['post_content']); - $topic_subject = validate($_POST['topic_subject']); - $topic_cat = validate($_POST['topic_cat']); - $user_id = validate($_SESSION['user_id']); - - create_topic($dbc, $topic_subject, $topic_cat, $user_id); - $topic_id = mysqli_insert_id($dbc); - create_post($dbc, $post_content, $topic_id, $user_id); - - if (!$post_result) { - echo 'An error occurred creating your post: ' . mysqli_error($dbc); - } - - header("Location: topic.php?id=" . $topic_id); -} - -?> - -<?php include_once 'footer.php';?>
\ No newline at end of file @@ -14,7 +14,7 @@ session_start(); <h1 id="title">cflip.net forum<sub style="font-size: small;">beta</sub></h1> <nav> <a href="index.php">Home</a> - <a href="create_topic.php">Create a topic</a> + <a href="create_thread.php">Create a thread</a> <div id="user"> <?php diff --git a/includes/db_inc.php b/includes/db_inc.php index b76f06d..2d157af 100644 --- a/includes/db_inc.php +++ b/includes/db_inc.php @@ -3,7 +3,7 @@ $db_server = 'localhost'; $db_user = 'root'; $db_pass = ''; -$db_database = 'forum'; +$db_database = 'forum2'; $dbc = mysqli_connect($db_server, $db_user, $db_pass, $db_database); diff --git a/includes/register_inc.php b/includes/register_inc.php deleted file mode 100644 index e69de29..0000000 --- a/includes/register_inc.php +++ /dev/null diff --git a/includes/reply_inc.php b/includes/reply_inc.php index 480c651..d20c4ed 100644 --- a/includes/reply_inc.php +++ b/includes/reply_inc.php @@ -10,10 +10,10 @@ if ($_SERVER['REQUEST_METHOD'] != 'POST') { } if (!isset($_SESSION['signed_in'])) { - die('You must be signed in to reply to a topic.'); + die('You must be signed in to reply to a thread.'); } -$sql = "INSERT INTO posts(post_content, post_date, post_topic, post_author) VALUES(?, NOW(), ?, ?)"; +$sql = "INSERT INTO posts(post_content, post_date, post_thread, post_author) VALUES(?, NOW(), ?, ?)"; $stmt = mysqli_stmt_init($dbc); if (!mysqli_stmt_prepare($stmt, $sql)) { @@ -24,4 +24,4 @@ mysqli_stmt_bind_param($stmt, "sii", $_POST['reply_content'], $_GET['reply_to'], mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); -header("Location: ../topic.php?id=" . $_GET['reply_to']);
\ No newline at end of file +header("Location: ../thread.php?id=" . $_GET['reply_to']);
\ No newline at end of file @@ -4,7 +4,7 @@ include_once 'header.php'; <table> <tr> <th class="left">Category</th> - <th class="right">Latest Topic</th> + <th class="right">Latest Thread</th> </tr> <?php include_once 'includes/db_inc.php'; @@ -20,7 +20,7 @@ include_once 'header.php'; echo '<tr><td class="left">'; echo '<h4><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h4>'; echo $row['cat_description']; - echo '</td><td class="right">Example topic right here<br><small>1 hour ago by <b>cflip</b></small></td></tr>'; + echo '</td><td class="right">Example thread<br><small>1 hour ago by <b>example user</b></small></td></tr>'; } mysqli_free_result($result); @@ -15,25 +15,25 @@ CREATE TABLE categories ( PRIMARY KEY (cat_id) ) ENGINE = InnoDB; -CREATE TABLE topics ( - topic_id INT(8) NOT NULL AUTO_INCREMENT, - topic_subject VARCHAR(255) NOT NULL, - topic_date DATETIME NOT NULL, - topic_cat INT(8) NOT NULL, - topic_author INT(8) NOT NULL, - PRIMARY KEY (topic_id) +CREATE TABLE threads ( + thread_id INT(8) NOT NULL AUTO_INCREMENT, + thread_subject VARCHAR(255) NOT NULL, + thread_date DATETIME NOT NULL, + thread_cat INT(8) NOT NULL, + thread_author INT(8) NOT NULL, + PRIMARY KEY (thread_id) ) ENGINE = InnoDB; CREATE TABLE posts ( post_id INT(8) NOT NULL AUTO_INCREMENT, post_content TEXT NOT NULL, post_date DATETIME NOT NULL, - post_topic INT(8) NOT NULL, + post_thread INT(8) NOT NULL, post_author INT(8) NOT NULL, PRIMARY KEY (post_id) ) ENGINE = InnoDB; -ALTER TABLE topics ADD FOREIGN KEY(topic_cat) REFERENCES categories(cat_id) ON DELETE CASCADE ON UPDATE CASCADE; -ALTER TABLE topics ADD FOREIGN KEY(topic_author) REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE; -ALTER TABLE posts ADD FOREIGN KEY(post_topic) REFERENCES topics(topic_id) ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE threads ADD FOREIGN KEY(thread_cat) REFERENCES categories(cat_id) ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE threads ADD FOREIGN KEY(thread_author) REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE posts ADD FOREIGN KEY(post_thread) REFERENCES threads(thread_id) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE posts ADD FOREIGN KEY(post_author) REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/topic.php b/topic.php deleted file mode 100644 index 751370b..0000000 --- a/topic.php +++ /dev/null @@ -1,67 +0,0 @@ -<?php include_once 'header.php'; ?> - -<?php -include_once 'includes/db_inc.php'; - -$sql = "SELECT topic_id, topic_subject, topic_date, user_id, user_name FROM topics LEFT JOIN users ON topic_author = user_id WHERE topic_id = " . mysqli_real_escape_string($dbc, $_GET['id']); -$result = mysqli_query($dbc, $sql); - -if (!$result) { - die('Error trying to display topic page: ' . mysqli_error()); -} - -if (mysqli_num_rows($result) == 0) { - echo 'This topic does not exist'; -} else { - while ($row = mysqli_fetch_assoc($result)) { - echo '<section><h1>' . $row['topic_subject'] . '</h1>'; - echo 'Created by <b>' . $row['user_name'] . '</b> on ' . date('M d, Y', strtotime($row['topic_date'])) . '</section>'; - $topic_id = $row['topic_id']; - } -} - -echo '</section>'; - -mysqli_free_result($result); - -$sql = "SELECT post_content, post_date, post_author, user_id, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_topic = " . mysqli_real_escape_string($dbc, $_GET['id']); -$result = mysqli_query($dbc, $sql); - -if (!$result) { - die('Error trying to display posts: ' . mysqli_error($dbc)); -} - -if (mysqli_num_rows($result) == 0) { - echo '<section>This topic has no posts</section>'; -} else { - echo '<table>'; - while ($row = mysqli_fetch_assoc($result)) { - echo '<tr class="post"><td class="right">Posted by <b>' . $row['user_name'] . '</b><br><small>' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '</small></td>'; - echo '<td class="left">' . $row['post_content'] . '</td></tr>'; - } - echo '</table>'; -} - -mysqli_free_result($result); - -if (isset($_SESSION['signed_in'])) { - echo ' - <section> - <form action="includes/reply_inc.php?reply_to=' . $topic_id .'>" method="post"> - <h2>Reply to this thread</h2> - <textarea name="reply_content"></textarea> - <br> - <input type="submit" name="submit"> - </form> - </section> - '; -} else { - echo ' - <section> - <a href="signin.php">Sign in</a> to reply to this thread</a> - </section> - '; -} - -include_once 'footer.php'; -?>
\ No newline at end of file |