Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Custom blog - How to store comments?

Featured Replies

Hello :)

 

I'm making a custom blog - nothing fancy just basic and wondered about adding comments. How would I go about doing this? Would I have a separate database for each blog entry?

 

Hopefully not, preferably I will be able to store them in the same database. What I'm looking to do (I hope this is possible) for every comment that is put into the database, for it to have an auto_increment but for each "BlogID".

 

So, for example when "BlogID" is 1, each row entry would go up by one for where "BlogID" is 1 ONLY, so when "BlogID" is 2, each row entry would go up by one also, but not in the same sequence, if you know what I mean.

 

I was thinking of storing in the same database, then getting the comments for each blog by doing

 

$comment_query = mysql_query("SELECT * FROM comments WHERE BlogID = '".$_GET['id']."'");

 

Heh, doubt I've explained that well... but I tried :p

 

Thank you.

Have a separate table, in this table (lets say 'blog_comments') you can have

 

id

blogid

username

email

comment

 

or something like this. the important part is blogid... When you do a query to show a blog post, then you also query the 'blog_comments' WHERE blogid is the ID of the blogpost in your blog table.

 

I think in your post you had the concept right. You can have many comment entries all over the place, as long as they have a way to point back to the blog post they're for.

  • Author

Okay, I've done something along those lines, but I'm having trouble displaying the comments :s Probably a simple problem, I'll go and have another look. If not, I'll post on here ;)

I think you need to do a little research into database theory.

 

Focus on what's known as "foreign keys".

 

ID fields are keys to a specific entry in a specific table of a database - they are identifiers.

 

When you want to link two seperate tables together (i.e. blog_entry and blog_comment or whatever you have) you store a foreign key to reference one table from the other.

 

Therefore you might have a database structure like this:

 

blog_entry

id (primary key)

author

text

date

 

blog_comment

id (primary key)

blog_entry_id (foreign key)

author

text

date

 

therefore each comment stores a reference to the blog entry it belongs to via the blog_entry_id foreign key.

 

This is the normal way of doing things when writing php and mySQL and allows you to do exactly what you are getting at. But if you are struggling, what you need to know are the proper names for what you are doing so you can google, or ask more specific questions!

  • Author

Thanks ErisDS, yea I know I'm terrible at explaining things which I'm not sure about... I've done what you've suggested, but I'm having a bit of trouble displaying them.

 

		$comment_query = mysql_query("SELECT * FROM comments WHERE BlogID = '".$_GET['id']."'");
	$row_comment = mysql_fetch_array($comment_query);
	while($comment = mysql_fetch_array($comment_query)) {
		echo "<p>".$row_comment['Comment']."</p>";
	}

 

Can you see anything wrong here?

No but then I have no idea what your data structure is.

 

It's not normal to use capitalisation like that though - usually lowercase and underscored is the norm.

  • Author

Ok, this is what it looks like.

 

fk1yx3.png

 

		//Comments
	$comment_query = mysql_query("SELECT * FROM comments WHERE BlogID = '".$_GET['id']."'");
	$row_comment = mysql_fetch_array($comment_query);
	while($comment = mysql_fetch_array($comment_query)) {
		echo "<p>".$row_comment['Comment']."</p>";
	}

 

I have 7 comment entries for BlogID 4 (not shown in picture above), and it's repeating "blog id 4" (5th entry shown above) 7 times, and I'm really not sure why. Any ideas?

  • Author

Uhh.. internet just went off, and Notepad++ crashed, I lost everything I had. Why... why.

 

Looks like I'm going to have to start from scratch... why would it delete it off the server? Urrghh :(

 

EDIT: :D :D :D :D :D :D It saved it locally ;)

Sorry, I didn't actually look at your code properly:

 

These lines don't make sense:

 

		
$row_comment = mysql_fetch_array($comment_query);
while($comment = mysql_fetch_array($comment_query)) {
 echo "<p>".$row_comment['Comment']."</p>";
}

 

Your fetching the results as an array, then you're fetching them again in a loop, but displaying only the original result...

 

This should work (variables renamed to be descriptive of what they contain):

		
$result = mysql_query("SELECT * FROM comments WHERE BlogID = '".$_GET['id']."'");
	while($row = mysql_fetch_array($result)) {
		echo "<p>".$row['Comment']."</p>";
	}

 

You also need to check to make sure that $_GET['id'] is returning what you expect

  • Author

Thank you so much ErisDS! :D

 

Also thanks ElanMan :)

You also need to check to make sure that $_GET['id'] is returning what you expect

 

I would look at cleaning you $_GET data.

 

To expand: $_GET will not always be what you expect. Any input that is EVER sent from a 3rd party such as another website, even HTML input fields with type="hidden". You can use functions such as

 

$variable = mysql_real_escape_string($_GET['variable']);

 

Another way to filter queries meant for database is great for when you want to optimize your pages because in best practice you should only ever connect to your database when you need too.

 

Security is very important when your sites with scripts are exposed to the public.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.