4

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

3
  • As Ian says, a refactoring might be in order. Keep in mind that storing the title with the id is going to end up being very problematic if you ever wish to edit titles. From what I can tell it might break the link in your chain between the entry id and title and wherever you're actually storing the entry data. Commented Nov 25, 2009 at 20:38
  • 1
    @Tchalvak: What exactly do you mean? Both the title and the contents are only stored once, so there should not be any integrity issues here... Commented Nov 25, 2009 at 20:43
  • Oh, it just came to mind that you might, just might be talking about storing the post with the title as filename? Commented Nov 25, 2009 at 20:43

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

7 Comments

ahem At least a warning that you should obviously take care of SQL injection issues...
PS - Take care of SQL injection issues.
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, might want to brush up =P
@OP: Check this question for example of vulnerabilities you should watch for (not only SQL injection): stackoverflow.com/questions/1783137/…
|
1

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.

1 Comment

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)
0

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

Comments

0

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.