jamcoupe Posted August 11, 2009 Share Posted August 11, 2009 I have two databases 'news' and 'comments' in the comments table I have a field called 'newsid' I am having problems inserting a new comment into the database. (I am not worrying about the formatting or what goes into the database yet, just trying to get things to work) news.php <?php require("db.php"); require_once("functions.php"); //DISPLAYS ARTICLE AND NEWS RELATIVE TO THE ID if(isset($_GET['article'])) { $news_id = get_news_id($_GET['article']); echo "{$news_id['title']}<br />"; echo "{$news_id['content']}<br />"; echo "{$news_id['date']}<br /><br />"; $get_comments = get_comments($news_id['id']); while ($comments = mysql_fetch_array($get_comments)) { if (!is_null($comments['newsid'])) { echo "{$comments['comment']}<br />"; } } //IF FORM IS FILLED IN ADD COMMENT TO DATABASE WHERE newsid if(isset($_POST['submit'])) { $id = $_GET['news']; $name = $_POST['name']; $comment = $_POST['comment']; $date = date('F \t\h\e jS, Y'); $query = ("INSERT INTO comments (newsid, name, comment, date) VALUE ('{$id}', '{$name}', '{$comment}', '{$date}')"); query_check($query); if (mysql_query($query, $connection)) { echo "Comment Added"; } else { echo "Comment Failed" . mysql_error; } } ?> <form action="news.php?articles=<?php $_GET['article']; ?>" method="post"> <input type="text" name="name" id="name" value="" /> <input type="text" name="comment" id="comment" value="" /> <input type="submit" name="submit" id="submit" value="Add Comment" /> </form> <?php } else { // DISPLAY NEWS TITLES WITH CLICKS TO GO TO FULL NEWS AND COMMENTS. $get_news = get_news(); while ($news = mysql_fetch_array($get_news)) { echo "<a href=\"news.php?article={$news['id']}\">{$news['title']}</a><br />"; } } include("dbclose.php"); ? functions.php <?php //FUNCTIONS function query_check($check) { if(!$check) { die ("OOOPS: " . mysql_error()); } } function get_news() { global $connection; $query = ("SELECT * FROM news ORDER BY id ASC"); query_check($query); $news = mysql_query($query, $connection); return $news; } function get_comments($id) { global $connection; $query = ("SELECT * FROM comments WHERE newsid = {$id} ORDER BY id ASC"); query_check($query); $comments = mysql_query($query, $connection); return $comments; } function get_news_id($news_id) { global $connection; $query = ("SELECT * FROM news WHERE id={$news_id} LIMIT 1"); $query = mysql_query($query, $connection); query_check($query); if($id = mysql_fetch_array($query)) { return $id; } else { return NULL; } } ?> Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 11, 2009 Share Posted August 11, 2009 Hi jamcoupe, The easiest way to do this is to post the news_id with the comment in a hidden input field, something like: <form action="news.php?articles=<?php $_GET['article']; ?>" method="post"> <input type="text" name="name" id="name" value="" /> <input type="text" name="comment" id="comment" value="" /> <input type="hidden" value="<?php addslashes(strtolower($_GET['article'])); ?>" name="newsid" /> <input type="submit" name="submit" id="submit" value="Add Comment" /> </form> Once that field is populated with the news id, just post it along with the comment, like: if(isset($_POST['submit'])) { $newsid = $_POST['newsid']; $name = $_POST['name']; $comment = $_POST['comment']; $date = date('F \t\h\e jS, Y'); $query = ("INSERT INTO comments (newsid, name, comment, date) VALUE ('{$newsid}', '{$name}', '{$comment}', '{$date}')"); Hope this helps. Quote Link to comment Share on other sites More sharing options...
jamcoupe Posted August 11, 2009 Author Share Posted August 11, 2009 I did as the above post asked and I still cant get it to work.. I get this error: OOOPS: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.