Jump to content

Comment form for news posts problems...


Guber-X

Recommended Posts

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>';

		}
	?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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">';

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

HURRAY!!!!

 

It now works great :D

 

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>';

		}
	?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.