# Allowing user comments on each article - PHP/MYSQL



## r3drock3t88 (Jan 13, 2007)

Hello everyone,

I am making a website just for practice with php / mysql, and I am trying to figure out how to setup a comments section.

There will be a bunch of articles on the homepage for example, and what I want is a link that says "Comment" right under the article and when clicked I want it to take the user to a page specifically designed for that article. However the articles are submitted by users, so I myself (the admin) won't be able to manually make each individual commenting page.

A great example of what I am trying to accomplish here is http://www.fmylife.com

That is the basic idea behind it. There are user submitted articles and a space for commenting.

Any help is MUCH appreciated  Thank you.

- Sam


----------



## amanxman (Mar 28, 2006)

Hey,

Basically for the commenting tool, you want a form which when submitted runs a script which INSERTS a new record in your SQL.

To do this, take a read of:

http://www.w3schools.com/Sql/sql_insert.asp

Basically, the form submission will submit to a php page with that INSERT script in it, and then that will add the comments record to the news story.

NOTE; you'll need to carefully think you're database structure as you'll need to be allow multiple comments to be attached to single stories.

Good luck


----------



## r3drock3t88 (Jan 13, 2007)

Hello again!

You've been a great help so far with all my posts, I really appreciate your time. I understand insert and how that all works, however, inserting multiple ones to each story is where I am confused.

I am going to think hard at it and try to figure this out on my own, but any suggestions or hints to point me in the right direction are always a big help.

Thanks again,

- Sam


----------



## amanxman (Mar 28, 2006)

Hey

You'll need two tables in your db...

a) Story
b) Comments

And then each record in the comments table links to a story, e.g.

STORY
Num
Title
Content

COMMENTS
Num
Content
Link Num

i.e. COMMENTS - Link Num is where you link each comment record to the news story.

And then on your actual website, you display the comments like this:

News Story 1
Content
Comments where 'Link Num' = 1 (do a foreach here to show all comments where news story number = 1)

News Story 2
Content
Comments where 'Link Num' = 2



So ultimatly it would work like:

FOREACH NEWS STORY
Title
Content
Comments:
FOR EACH COMMENT WHERE LINK NUM = NUM
Comments content
END FOREACH
END FOREACH

Does that make sense? Sorry not got time to write full php code, but hopefully that points u in right direction...

Start off with the db sctucture, geting it to allow multiple comments to each story, and then once you have that, worry about the front end comment inputting form.


----------



## r3drock3t88 (Jan 13, 2007)

Thanks for the detailed reply.

I understand the concept you're getting at here, but would I have any use for the "num" part in the comments section? The "link num" in COMMENTS seems to be linking to "num" in the stories, so that's all I would need to check for, correct?

Also, say if I wanted the comments on their own page such that when the user clicks the "view comments" button, it goes to a separate page where the article is at the top, then comments are directly below it. That is the overall goal I would love to have.

Thanks again, I really appreciate all your help. It's tough learning this stuff but awesome when you have someone helping like you are.


----------



## amanxman (Mar 28, 2006)

Hey,

Yeah correct, the num within the comments table is just the ID number for the comments record. You'll be assigning each comment record to a story via the link num.

As for your second point, that is what I forsee happening here anyway, is it not?! The view comments button can simply take the user to the detail page of the story, which displays the appropriate single story, following by each comment record where link num = (story) num.

Does that make sense?


----------



## r3drock3t88 (Jan 13, 2007)

Ahh yes yes, very good! That does seem to be what the overall outcome would be. I will try and put it all together in a little bit!

Thanks so much for your help, and I will post back with an update of how things are coming along.


----------



## r3drock3t88 (Jan 13, 2007)

Here is what I have so far ...

On the homepage, all the articles get printed to the screen and there is a link at the bottom stating "Comments". Once clicked, it takes the user to another page with a link like the example...

"/commentspage.php?comment=linkNumberHere"

This is all done using this code


```
<a href=\"commentspage.php?comments=$linkNum\">Comments</a>
```
And to get the "$linkNum" I simply accessed the database where the stories are stored like you said, and retrieved the 'id' from that to use it as the link number for the comments.

Here is the code for the comments page...


```
$storyID = $_GET['comments'];						//Get ID from previous page
echo "storyID = " . $storyID;						//Print ID for test (prints 35 if story ID == 35)

$conn = mysql_connect($dbhost, $dbuser, $dbpass);	//Connect , print error if failure
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

//Connect to database, print failure if needed
mysql_select_db('userInfo') or die('Could not connect to db' . mysql_error());

$sql = "SELECT * FROM comments WHERE linkNum=35 ";	//Select all information from comments table where linkNum == storyID

$info = mysql_query( $sql, $conn );					//Query the data
if(! $info )
{
  die('Could not get data: ' . mysql_error());		//If error during query, says so
}

$row = mysql_fetch_array($info, MYSQL_NUM );		//Fetch the data from $info
print "Comment: " . $row['comment'];				//print the comment stored at that position

//NOTE: Nothing prints out wen using '$row['comment'];
```
Sorry if it is a little sloppy, but I feel like everything should be correct. I have a test at the top to make sure that the proper link number is being printed, which it is. If the ID of the story is 35, then the link states "commentspage.php?comments=35" and in my commentspage.php as you can see it retrieves the ID using the get function.

I print it out to test it and make sure it works, and it does. Then I retrieve from the comments database all the information where the linkNum variable in the database is equal to the storyID that is retrieved from the URL.

However, at the bottom when I try printing it, nothing comes out! I don't know why. I have a test comment stored in the database with a linkNum of 35 just to test it out, but still nothing shows.

Do you see anything that I may be doing wrong? Thank you sooo much!


----------



## awatson (Jan 29, 2008)

Try this:


```
$sql = "SELECT * FROM comments WHERE linkNum=35 ";	//Select all information from comments table where linkNum == storyID

$info = mysql_query( $sql, $conn); //Query the data
while ($record = mysql_fetch_object($info)) { 
  // output the comment here, for example:
  echo "

$record->Content

";
}
```
I used fetch_object to grab and output each returned result. If this were for a site that's more than just for practice, you'd probably want to add lot of other fields to the comment table such as date_posted, the poster's email address, etc. You may even need to set up a moderation system so that comments that aren't approved aren't shown, etc. But it's a good start!


----------



## r3drock3t88 (Jan 13, 2007)

Thank you for your help.

I tried the fetch object code you gave me but still for some reason I get no output and I cannot figure out why. I don't understand what is wrong with my code. It should simply connect to the comments database, find the linkNum column, get all the information in each row WHERE linkNum is equal to 35, then return that information for use, right?

I don't understand why when I go to print the results nothing shows :-/. I have a test row in my comments too, so I know the information is all there.


----------



## awatson (Jan 29, 2008)

Hmm... try putting an echo statement inside the while loop to see if the code in that loop getting run... If not, there may be a problem with the SQL query. You can test that my copying the query from the PHP and pasting it into the mysql console program (after connecting to the database), or phpmyadmin.


----------



## r3drock3t88 (Jan 13, 2007)

Got it! I made a stupid little mistake haha, ahh the frustrations of programming =] but thank you very much for the help. 

Do you know why fetch array wouldnt work but fetch object did? I wouldn't mind knowing what the cause of that was.


----------

