views:

170

answers:

4

Hi guys! I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.

Right now I have a code that takes and displays the comment, but the problem is coordinating which comments go on which post. My current set up is all my post are HTML files, and the comments are stored in a database. I also have a form that creates a new row with a unique post ID and title for each post.

My basic DB setup right now is as follows: 1 database, 2 tables. A post table and a comments table. In the comments table I have the general name, website, comment, etc. and I also have a unique ID that auto-increments for each comment. Then I have a post_id which should match up with the designated post.

On the post table, I have just two fields: entry_id and title. The title is manually set by me and the entry_id is auto-incremented. NOTE: The entry itself is NOT stored in the database.

So my current problem is how to set the post_id for each page of comments and how to associate the entry_id with the actual post. I hope that's not too confusing. Thanks a ton for any help!

-iMaster

+7  A: 

I think that you should consider refactoring your code to store the post in your database.

From there, you'd have a page (http://mysite/showpost.php?post_id=5) that displays your post (psuedo-code'ish):

<?php

// establish database connection here

// Simple SQL injection prevention:
foreach ($_REQUEST as $key => $value)
{
  $_REQUEST[$key] = mysql_real_escape_string($value);
}

// Get the appropriate post from the posts table.
$query = "SELECT post FROM posts WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);
$row = mysql_fetch_assoc($query);

echo $row['posts'];

// Get the appropriate comments from the comments table.
$query = "SELECT comment FROM comments WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
  echo "Comment: {$row['comment']}";
}

// close connections, etc.    

?>

My PHP is very rusty, but this should give you a good idea of the data structure and code needed to accomplish what you want.

Ian P
*ahem* At least a warning that you should obviously take care of SQL injection issues...
Franz
PS - Take care of SQL injection issues.
Ian P
Good job, Ian ;)
Franz
SQL injection aside, I've coded php for years and I've never seen curly braces in a string for interpolation. COOL! Now I don't have to concat strings when I want ot use an array. Love SO learn somthing new every day! +1! (good answer too)
Byron Whitlock
Byron, might want to brush up =P
Joe Philllips
@OP: Check this question for example of vulnerabilities you should watch for (not only SQL injection): http://stackoverflow.com/questions/1783137/examples-of-vulnerable-php-code
Carlos Lima
There are two reasons that I'm trying to avoid this method:1. I think that the URL's you get are ugly and confusing to those who aren't tehc enthusiasts2. I need to be able to control the formatting and layout of each post individually because my layout may differ for each post. If posts are stored in a DB it takes away a lot of that freedom.
WillyG
It's not like you can't store HTML in a database...
Franz
iMaster - take it one step at a time. Get this working, and then investigate an MVC framework with mod_rewrite (or similar if you're not using apache.) That will give you the ability to use pretty URLs, http://mysite/posts/1
Ian P
A: 

You should follow Ian's advise and refactor your code to use a table. Otherwise you will need to hard code some PHP when you create the post html.

like

$actualPostId = 1234; // you get this from the database
file_put_contents ($filename, "<?php \$postID= $actualPostId;?> $rest_of_html");
Byron Whitlock
You have a syntax error :D
Franz
@Franz, Thanks.
Byron Whitlock
+1  A: 

Good for you for learning to "roll your own" and get exactly what you want and learn something along the way.

You should create a table for comments with a foreign key that matches the article ID.

Then when displaying your comments, do a query to fetch all comments associated with that article ID.

Jeremy Morgan
I like you're idea here, as it is something I tried before. The problem that keeps daunting me is that is there anyway to set the entry ID (the one that is referenced by the post id on comments) without hard coding it? How would I do that? Forgive me if its a "stupid question" but I'm new to PHP (as you can probably tell)
WillyG
A: 

if i am reading the problem right, i think you just need to add the entry_id to the comment form as a hidden field and when someone posts a comment, include it as post_id when you insert into the comment table. i think then you have the missing link between tables.

mtvee
That is one solution and a valid one. Ideally, though I'd like this to be automatically set for me rather than me having to set it manually each time.
WillyG