I am getting an error message when I try to call some code. It's my first forum. The message is:
MYSQL syntax error near " in line 1, check what version you're using
Or something similar I forgot to copy the exact message but thats it more or less.
The error is in reply to post:
<?php
//connect to the server and select database we'll need it soon
$conn = mysql_connect("localhost") or die(mysql_error());
mysql_select_db("firstforum",$conn) or die(mysql_error());
//check to see if we're showing the form or adding the post
if ($_POST[op] != "addpost") {
//showing the form; check for required item in query string
if (!$_GET[post_id]) {
header("Location: topiclist.php");
exit;
}
//still have to verify topic and post
$verify = "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 = mysql_query($verify,$conn) or die(mysql_error());
if (mysql_num_rows($verify_res) < 1 ) {
//this post or topic does not exist
header("location: topiclist.php");
exit;
} else {
//get the topic id and title
$topic_id = mysql_result($verify_res,0,'topic_id');
$topic_title = stripslashes(mysql_result($verify_res,0,'topic_title'));
print "<html><head><title>Post your reply in $topic_title</title></head><body><h1>Post your reply in $topic_title</h1><form method=post action=\"$_SERVER[PHP_SELF]\"><p><strong>Your email address:</strong><br><input type=\"text\" name=\"post_owner\" size=40 maxlength=150><p><strong>Post Text:</strong><br><textarea name=\"post_text\" rows=8 cols=40 wrap=virtual></textarea><input type=\"hidden\" name=\"op\" value=\"addpost\"><input type=\"hidden\" name=\"topic_id\" value=\"$topic_id\"><p><input type=\"submit\" name=\"submit\" value=\"add Post\"></p></form>
</body></html>";
}
} else if($_POST[op] == "addpost") {
//check for required items from form
if ((!$_POST[topic_id]) || (!$_POST[post_text]) || (!$_POST[post_owner])) {
header("Location: topiclist.php");
exit;
}
//add the post
$add_post = "insert into forum_posts values ('', '$_POST[topic_id]', '$_POST[post_text]', now(), '$_POST[post_owner]')";
mysql_query($add_post,$conn) or die(mysql_error());
//redirect user to topic
header("Location: showtopic.php?topic_id=$topic_id");
exit;
}
?>
The above code is called from the following, if it makes any difference.
<?php
//check for required info from the query string
if (!$_GET[topic_id]) {
header("Location: topiclist.php");
exit;
}
//connect to server and select database
$conn = mysql_connect("localhost") or die(mysql_error());
mysql_select_db("firstforum",$conn) or die(mysql_error());
//verify the topic exists
$verify_topic = "select topic_title from forum_topics where topic_id = $_GET[topic_id]";
$verify_topic_res = mysql_query($verify_topic, $conn) or die(mysql_error());
if (mysql_num_rows($verify_topic_res) < 1) {
//this topic does not exist
$display_block = "<p><em>You have selected an invalid topic. Please <a href=\"topiclist.php\">try again</a>.</em></p>";
} else {
// get the topic title
$topic_title = stripslashes(mysql_result($verify_topic_res,0,'topic_title'));
//gather the posts
$get_posts = "select post_id, post_text, date_format(post_create_time,'%b %e %Y at %r') as fmt_post_create_time, post_owner from forum_posts where topic_id = $_GET[topic_id] order by post_create_time asc";
$get_posts_res = mysql_query($get_posts, $conn) or die(mysql_error());
//create the display string
$display_block = "<p>Showing posts for the <strong>$topic_title</strong> topic:</p><table width = 100% cellpadding=3 cellspacing=1 border=1><tr><th>AUTHOR</th><th>POST</th></tr>";
while ($posts_info = mysql_fetch_array($get_posts_res)) {
$post_id = $posts_info['post_id'];
$post_text = nl2br(stripslashes($posts_info['post_text']));
// $post_text = "Yo, post text!";
$post_create_time = $posts_info['fmt_post_create_time'];
$post_owner = stripslashes($posts_info['post_owner']);
//add to display
$display_block .= "<tr>";
$display_block .= "<td width=35% valign=top>$post_owner<br>[$post_create_time]</td>";
$display_block .= "<td width=65% valign=top>$post_text<br><br>";
$display_block .= "<a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO POST";
$display_block .= "</strong></a></td></tr>";
}
//close up the table
$display_block .= "</table>";
}
?>
<html>
<head>
<title>Posts in Topic</title>
</head>
<body>
<h1>Posts in Topic</h1>
<?php print $display_block; ?>
</body>
</html>
Appreciate any suggestions.
Gregg