Jump to content

problem with SELECT


kleb

Recommended Posts

i need help with this issue.i designed a comment page where people can comment on a topic but what i descovered is that once i comment on a topic it will be showed on the page i asked it to go but if i comment on thesame topic it will not show exceeptb the one that as been there before.even if i click on anotherr topic and i comment once i cannot comment again but if i remove the WHERE clause all the comments meant for other topics will just display which is not good.Please help me

thanks. this is my code:

<?php 
			include"header.php";
			$topicid=$_GET['id'];
			///the two tables POST and TOPICS share thesame id
			$sql="SELECT post_comments FROM post WHERE $topicid=topicsID";
			$result=mysql_query($sql) or die(mysql_error());
			while($row=mysql_fetch_array($result))
			     {
                 		echo"{$row['post_comments']}";
					}
					   ?>

Link to comment
Share on other sites

:'(can any one give me the right codes?guys i want to add a comment page to some articles on my site.i have created a table where the comments will go and when a visitor clicks on any of the articles it will show the comments by previous visitors.pls note that i dont want comment made for article a to sbow in article b..just like php freaks.thanks.happy newyear to you.

Link to comment
Share on other sites

i wrote a code b4 but since it just displayed all the comments,i deleted it

 

How do you want the comments displayed?  By date, member, or just a hard number?

 

I'll be honest with you-if you're just looking for Blog-style comments,  the easiest thing you could do is install Wordpress and let it handled the comments for you.  Barring that, create a Date field in your table that holds the comments, and then grab a date range to display.  If you could be a little more specific, I could probably post some examples for you.  Is the only parameter that the comments can't appear on the same page as the article?  Because that's very, very easy.  All you have to do is build a discreet link for each article, and link out to it from a "view comments" button.  I have to believe that there's more to your request, because it would literally be more work to make the comments appear on the same page with the articles than it would be to have them appear on a separate page.

Link to comment
Share on other sites

mavent you are the only one that understood me well..yes i want the comments to apear on the article page and not in another page.one thing i discover was when i tried to display the comments by id,i.e i said WHERE articleid=commentid (i joined the two tables with a common id called articleid)it should display the records but what i discover is that it will only display one comment and leave the rest although i understand why that happend but i dont know what to write in the WHERE clause so that other comments made on an article will appear on that same article page.am replying you via mobile so i cant post a code now

thanks

Link to comment
Share on other sites

We understood you fine, but we don't provide free scripts here. We help you to finish your own

 

This.  We're here to help, not do all of your work for you.  If you need help, show us some code.  If you simply want someone to do all the work for you, post in the 'Freelance' section.

Link to comment
Share on other sites

the field name needs to be before the value.

 

$sql="SELECT post_comments FROM post WHERE $topicid=topicsID"; // reverse $topicsID and $topicid
$sql="SELECT post_comments FROM post WHERE topicsID =  $topicid"; //correct

Link to comment
Share on other sites

This is his code:

reply.php

<?php
include"header.php";
if(isset($_POST['submit']))
{
$comment=mysql_real_escape_string(trim($_POST['comment']));
$name=mysql_real_escape_string(trim($_POST['name']));
if($comment!=='' && $name!=='')
{
	$ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')";
	mysql_query($ins) or die(mysql_error());
if($ins)echo"succesfull, you can click back to view your comment and other new comments</a>";
}
else
{
	echo"You can not post an empty page or leave your name blank";
}
}
?>

 

comments.php

<?php
include"header.php";
$topicid=$_GET['id'];
///please note that i joined the two tables POST and TOPICS with a common id(topicsID);
$sql="SELECT post_content,post_by FROM post WHERE $topicid=topicsID";
$result=mysql_query($sql)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
}

?>

 

So. This is wrong:

$sql="SELECT post_content,post_by FROM post WHERE $topicid=topicsID";

 

It should be:

$sql="SELECT post_content,post_by FROM post WHERE topicsID='$topicid'";

 

In reply.php you don't enter the topic ID, only the content and name...so you can never retrieve it.

 

And you don't escape or validate the $topicid in comments.php.

Link to comment
Share on other sites

This is his code:

reply.php

<?php
include"header.php";
if(isset($_POST['submit']))
{
$comment=mysql_real_escape_string(trim($_POST['comment']));
$name=mysql_real_escape_string(trim($_POST['name']));
if($comment!=='' && $name!=='')
{
	$ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')";
	mysql_query($ins) or die(mysql_error());
if($ins)echo"succesfull, you can click back to view your comment and other new comments</a>";
}
else
{
	echo"You can not post an empty page or leave your name blank";
}
}
?>

 

comments.php

<?php
include"header.php";
$topicid=$_GET['id'];
///please note that i joined the two tables POST and TOPICS with a common id(topicsID);
$sql="SELECT post_content,post_by FROM post WHERE $topicid=topicsID";
$result=mysql_query($sql)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo"<strong>{$row['post_by']}</strong>:  {$row['post_content']}"."</br>";
}

?>

 

So. This is wrong:

$sql="SELECT post_content,post_by FROM post WHERE $topicid=topicsID";

 

It should be:

$sql="SELECT post_content,post_by FROM post WHERE topicsID='$topicid'";

 

In reply.php you don't enter the topic ID, only the content and name...so you can never retrieve it.

 

And you don't escape or validate the $topicid in comments.php.

i actually commented on the same error in another one of his threads.

Kleb, please do not post multiple threads on the same topic.

Link to comment
Share on other sites

the field name needs to be before the value.

 

$sql="SELECT post_comments FROM post WHERE $topicid=topicsID"; // reverse $topicsID and $topicid
$sql="SELECT post_comments FROM post WHERE topicsID =  $topicid"; //correct

 

While convention has you list the column first, it will work either way.

Link to comment
Share on other sites

the field name needs to be before the value.

 

$sql="SELECT post_comments FROM post WHERE $topicid=topicsID"; // reverse $topicsID and $topicid
$sql="SELECT post_comments FROM post WHERE topicsID =  $topicid"; //correct

 

While convention has you list the column first, it will work either way.

learned something new then

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.