Guber-X Posted January 13, 2012 Share Posted January 13, 2012 as far as everything goes, i have it working but not quite perfect yet... you may assume already what im trying to do, and yes im making a comment box option for each news posting I have. I have it working up to the point of showing comment posts and posting them. But my issue is, anytime i submit a comment for one news post, it will repeat the post for how many news postings i have and will place them in each news posting. so i need some help to prevent this lol. here's my code <?php //News Code Here $dbtn = 'news'; $result = mysql_query("SELECT * FROM $dbtn ORDER by id DESC LIMIT 10") or die("query failed: " . msql_error()); while ($row = mysql_fetch_array($result)) { list($id, $header, $date, $news, $edate) = $row; $news = nl2br($news); $date = date("M j, Y",strtotime("$date")); echo '<table width="680" border="0" cellpadding="14" cellspacing="0">'; echo '<tr>'; echo '<td class="newspost">'; echo '<font size="+2"><b>'.$header.'</b></font><font size="-1" color="#CCCCCC"> - '.$date.'</font><br />'; echo '<hr color="#FFFFFF" width="100" align="left" size="1">'; echo ' - '.$news.$edate; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td>'; $comres = mysql_query("SELECT * FROM comment WHERE menu_title='$dbtn' AND post_id='$id' ORDER by date DESC") or die("query failed: " . msql_error()); while($rows = mysql_fetch_array($comres)){ list($comid, $menu_title, $post_id, $comdate, $comname, $comment) = $rows; $comment = nl2br($comment); $comdate = date("M j, Y",strtotime("$comdate")); echo 'User: '.$comname.'<br />'; echo ' - '.$comment; echo '<div align="right"><font color="grey">'.$comdate.'</font></div>'; } echo '<a class="show_hide" rel="#slidingDiv_'.$id.'">Comment</a>'; echo '<div id="slidingDiv_'.$id.'" class="toggleDiv" style="display: none;">'; if(isset($_POST["add"])){ $name = $_POST["comname"]; $comment = $_POST["comment"]; $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', '$id', CURDATE(), '$name', '$comment')"; mysql_query($querys) or die("Error, insert query failed: ".mysql_error()); echo '<br /><br />Thank You!'; } else { echo '<form method="post">'; echo '<table width="100%" border="0" cellspacing="1" cellpadding="2">'; echo '<tr>'; echo '<td align="left" valign="top">Name</td>'; echo '<td align="left" valign="top">Comment</td>'; echo '</tr>'; echo '<tr>'; echo '<td align="left" valign="top"><input name="comname" size="25" type="text" id="comname"></td>'; echo '<td align="left" valign="top"><textarea name="comment" id="comment" cols="40" rows="4"></textarea></td>'; echo '</tr>'; echo '<tr>'; echo '<td> </td>'; echo '<td> </td>'; echo '</tr>'; echo '<tr>'; echo '<td> </td>'; echo '<td align="right" valign="top"><input name="add" type="submit" id="add" value="Submit"></td>'; echo '</tr>'; echo '</table>'; echo '</form>'; echo '</div>'; } echo '</td>'; echo '</tr>'; echo '</table>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/ Share on other sites More sharing options...
Muddy_Funster Posted January 13, 2012 Share Posted January 13, 2012 You have no "WHERE" clause on your insert. Add "WHERE id = $id" to that and you should be set Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307330 Share on other sites More sharing options...
Guber-X Posted January 13, 2012 Author Share Posted January 13, 2012 i do see how that could be an issue, i tried it and i get a error "Error, insert query failed: 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 'WHERE id='3'' at line 1" thing is i dont understand how its suppose to match 'id' tags from a different query. cuz the "WHERE id=$id" is trying to match "id" tags from the "comment" table where it needs to match it with the "news" table Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307366 Share on other sites More sharing options...
litebearer Posted January 13, 2012 Share Posted January 13, 2012 encapsulate $id in single quotes ie '$id' Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307367 Share on other sites More sharing options...
Guber-X Posted January 13, 2012 Author Share Posted January 13, 2012 encapsulate $id in single quotes ie '$id' i do have it like that Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307373 Share on other sites More sharing options...
Guber-X Posted January 13, 2012 Author Share Posted January 13, 2012 here are the ways ive tried... <?php $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', '$id', CURDATE(), '$name', '$comment') SELECT id FROM news WHERE news.id='$id'"; mysql_query($querys) or die("Error, insert query failed: ".mysql_error()); ?> <?php $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', '$id', CURDATE(), '$name', '$comment') WHERE id='$id'"; mysql_query($querys) or die("Error, insert query failed: ".mysql_error()); ?> <?php $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', '$id', CURDATE(), '$name', '$comment') WHERE news.id='$id'"; mysql_query($querys) or die("Error, insert query failed: ".mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307377 Share on other sites More sharing options...
Muddy_Funster Posted January 13, 2012 Share Posted January 13, 2012 what field of the comment table refferences the news table? Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307392 Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2012 Share Posted January 13, 2012 INSERT queries do not use WHERE clauses. You either INSERT a new record, or UPDATE an existing record. Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307398 Share on other sites More sharing options...
Guber-X Posted January 14, 2012 Author Share Posted January 14, 2012 what field of the comment table refferences the news table? post_id references the news table using the id column here are how my tables are laid out... news - id - header - date - news - edate (aka edit date) comment - id - menu_title (used for what section the post is to) - post_id (matches the post id to organize comments together properly) - date - name - comment INSERT queries do not use WHERE clauses. You either INSERT a new record, or UPDATE an existing record. you are incorrect. the INSERT query do accept the WHERE clause if you do a bit of research. reference from here http://www.techonthenet.com/sql/insert.php Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307439 Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2012 Share Posted January 14, 2012 Pikachu2000 is absoloutly right, the use of where in the link that you posted is within a select subquery. I got mixed up with update because I hade an "oops" moment with that the other week there. There is something sneeky going on, your code looks to be fine on the surface, is the data in your table accurate to how you think it should be? Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307444 Share on other sites More sharing options...
Guber-X Posted January 14, 2012 Author Share Posted January 14, 2012 ah, okay makes sence. as far as the data goes in my tables its all how it should be. here is the data from my tables (they are from my test server). News Table: id - header - date - news - edate 1 - Gather Data from Database - 2011-10-24 - [bLOB - 52B] - NULL 2 - Page Scroll Test - 2011-10-25 - [bLOB - 514B] - NULL 3 - More date testing for Feature Box - 2011-11-07 - [bLOB - 27B] - NULL Comment Table: id - menu_title - post_id - date - name - comment 1 - news - 3 - 2012-01-12 - GuberX - [bLOB - 23B] so from data the comment post is only suppose to show under news ID 3... but when i submit a comment that i right for news ID 3 it will repeat the submit for how many news postings there are. for when it does this it will show 3 new postings in the data table and the post_id will go 3-2-1.... where it should only be one post and should show post_id 3 Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307447 Share on other sites More sharing options...
Pikachu2000 Posted January 14, 2012 Share Posted January 14, 2012 INSERT queries do not use WHERE clauses. You either INSERT a new record, or UPDATE an existing record. you are incorrect. the INSERT query do accept the WHERE clause if you do a bit of research. reference from here http://www.techonthenet.com/sql/insert.php If you read the examples in the page you linked to, you'll see why I'm not incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307476 Share on other sites More sharing options...
Guber-X Posted January 14, 2012 Author Share Posted January 14, 2012 INSERT queries do not use WHERE clauses. You either INSERT a new record, or UPDATE an existing record. you are incorrect. the INSERT query do accept the WHERE clause if you do a bit of research. reference from here http://www.techonthenet.com/sql/insert.php If you read the examples in the page you linked to, you'll see why I'm not incorrect. yes i do see... Muddy_Funster explained it Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307487 Share on other sites More sharing options...
laffin Posted January 14, 2012 Share Posted January 14, 2012 Because your insert is within the loop and there is no postid check, so you should have multiple inserts and you dont need '' around $id in the queries because they should be numeric if (isset($_POST["add"]) && $_POST['postid']=$id) { $name = $_POST["comname"]; $comment = $_POST["comment"]; $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', $id, CURDATE(), '$name', '$comment')"; mysql_query($querys) or die("Error, insert query failed: " . mysql_error()); echo '<br /><br />Thank You!'; } else { echo '<form method="post"><input type="postid" value="$id">'; Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307528 Share on other sites More sharing options...
Guber-X Posted January 14, 2012 Author Share Posted January 14, 2012 Because your insert is within the loop and there is no postid check, so you should have multiple inserts and you dont need '' around $id in the queries because they should be numeric if (isset($_POST["add"]) && $_POST['postid']=$id) { $name = $_POST["comname"]; $comment = $_POST["comment"]; $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', $id, CURDATE(), '$name', '$comment')"; mysql_query($querys) or die("Error, insert query failed: " . mysql_error()); echo '<br /><br />Thank You!'; } else { echo '<form method="post"><input type="postid" value="$id">'; well i didnt know any other way on how to put the comment form to every news posting... and i tried ur little code posting there, and the same thing still happens... get multiple comment posts Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307621 Share on other sites More sharing options...
laffin Posted January 14, 2012 Share Posted January 14, 2012 oops change the =$id to ==$id Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307634 Share on other sites More sharing options...
Guber-X Posted January 14, 2012 Author Share Posted January 14, 2012 okay, now it doesnt post, doesnt show now errors... :/ lol Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307649 Share on other sites More sharing options...
Guber-X Posted January 14, 2012 Author Share Posted January 14, 2012 HURRAY!!!! It now works great i had to fix a few things from the help i received from fellow posters in this topic here is how the code looks now: p.s. look for the yellow quotes to see where i actually changed code to make it work lol <?php //News Code Here $dbtn = 'news'; $result = mysql_query("SELECT * FROM $dbtn ORDER by id DESC LIMIT 10") or die("query failed: " . msql_error()); while ($row = mysql_fetch_array($result)) { list($id, $header, $date, $news, $edate) = $row; $news = nl2br($news); $date = date("M j, Y",strtotime("$date")); echo '<table width="680" border="0" cellpadding="14" cellspacing="0">'; echo '<tr>'; echo '<td class="newspost">'; echo '<font size="+2"><b>'.$header.'</b></font><font size="-1" color="#CCCCCC"> - '.$date.'</font><br />'; echo '<hr color="#FFFFFF" width="100" align="left" size="1">'; echo ' - '.$news.$edate; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td>'; $comres = mysql_query("SELECT * FROM comment WHERE menu_title='$dbtn' AND post_id='$id' ORDER by date DESC") or die("query failed: " . msql_error()); while($rows = mysql_fetch_array($comres)){ list($comid, $menu_title, $post_id, $comdate, $comname, $comment) = $rows; $comment = nl2br($comment); $comdate = date("g:ia - M j, Y",strtotime("$comdate")); echo 'User: '.$comname.'<font color="#B20303"> - '.$comdate.'</font><br />'; echo ' - '.$comment.'<br /><br />'; } echo '<a class="show_hide" rel="#slidingDiv_'.$id.'">Leave a Comment</a>'; echo '<div id="slidingDiv_'.$id.'" class="toggleDiv" style="display: none;">'; if (isset($_POST["add"]) && $_POST['postid']==$id) { //added postid==$id $name = $_POST["comname"]; $comment = $_POST["comment"]; $querys = "INSERT INTO comment (menu_title, post_id, date, name, comment) VALUES ('$dbtn', $id, NOW(), '$name', '$comment')"; mysql_query($querys) or die("Error, insert query failed: " . mysql_error()); echo '<br /><br />Thank You!'; } else { echo '<form method="post"><input name="postid" type="hidden" value="'.$id.'">'; //added input postid fixed type to hidden and added name echo '<table width="100%" border="0" cellspacing="1" cellpadding="2">'; echo '<tr>'; echo '<td align="left" valign="top">Name</td>'; echo '<td align="left" valign="top">Comment</td>'; echo '</tr>'; echo '<tr>'; echo '<td align="left" valign="top"><input name="comname" size="25" type="text" id="comname"></td>'; echo '<td align="left" valign="top"><textarea name="comment" id="comment" cols="40" rows="4"></textarea></td>'; echo '</tr>'; echo '<tr>'; echo '<td> </td>'; echo '<td> </td>'; echo '</tr>'; echo '<tr>'; echo '<td> </td>'; echo '<td align="right" valign="top"><input name="add" type="submit" id="add" value="Submit"></td>'; echo '</tr>'; echo '</table>'; echo '</form>'; echo '</div>'; } echo '</td>'; echo '</tr>'; echo '</table>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307653 Share on other sites More sharing options...
laffin Posted January 14, 2012 Share Posted January 14, 2012 Ahhh glad when i have typos and mistakes, and user is able to fix em Gratz Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307655 Share on other sites More sharing options...
Guber-X Posted January 15, 2012 Author Share Posted January 15, 2012 Ahhh glad when i have typos and mistakes, and user is able to fix em Gratz thanx for the help Quote Link to comment https://forums.phpfreaks.com/topic/254965-comment-form-for-news-posts-problems/#findComment-1307895 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.