// connect to server
$mysqli = mysqli_connect('XXXX');
// check to see if we're showing the form or adding the post
if (!isset($_POST['topic_id'])) {
// showing the form; check for required item in query string
if (!isset($_GET['post_id'])) {
header('Location: topiclist.php');
exit;
}
// still have to verify topic and post
#! This is very very bad fix it or be faced with SQL Injection
$verify_sql = 'SELECT ft.topic_id, ft.topic_title FROM forum_posts AS fp LEFT JOIN forum_topics AS ft ON fp.topic_id = ft.topic_id WHERE fp.post_id = "' . $_GET['post_id'] . '"';
$verify_res = mysqli_query($mysqli, $verify_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($verify_res) < 1) {
// this post or topic does not exist
header('Location: topiclist.php');
exit;
} else {
// get the topic id and title
while($topic_info = mysqli_fetch_array($verify_res)) {
$topic_id = $topic_info['topic_id'];
$topic_title = stripslashes($topic_info['topic_title']);
}
echo <<<HTML
<html>
<head>
<title>Post Your Reply in $topic_title</title>
</head>
<body>
<p><strong>Post Your Reply in $topic_title</strong></p>
<form method="post" action="{$_SERVER['PHP_SELF']}">
<input type="hidden" name="topic_id" value="{$topic_id}">
<p><strong>Your Name/strong><br/><input type="text" name="post_owner" size="40" maxlength="150"></p>
<p><strong>Post Text/strong><br/><textarea name="post_text" rows="8" cols="40" wrap="virtual"></textarea></p>
<p><input type="submit" name="submit" value="Add Post"></p>
</form>
</body>
</html>
HTML;
}
mysqli_free_result($verify_res);
mysqli_close($mysqli);
} else if (isset($_POST['topic_id'])) {
// check for required items from form
if (!isset($_POST['topic_id']) || !isset($_POST['post_text']) || !isset($_POST['post_owner'])) {
header('Location: topiclist.php');
exit;
}
// add the post
#! This is very very bad fix it or be faced with SQL Injection
$add_post_sql = 'INSERT INTO forum_posts (topic_id, post_text, post_create_time, post_owner) VALUES ("' . $_POST['topic_id'] . '", "' . $_POST['post_text'] . '", now(), "' . $_POST['post_owner'] . '")';
$add_post_res = mysqli_query($mysqli, $add_post_sql) or die(mysqli_error($mysqli));
mysqli_close($mysqli);
// redirect user to topic
header('Location: showtopic.php?topic_id=' . $_POST['topic_id']);
exit;
}
Bookmarks