PHP PHP Code Help

I'm in the process of coding my very first blog. With the help of various textbooks and other forums, I managed to compile a semi-working code.

Right now I have a code that accepts and displays a comment, but the problem is that it coordinates which comments follow that message. My current setup - all my posts are HTML files, and comments are stored in a database. I also have a form that creates a new line with a unique message identifier and a header for each message.

My basic database setup now looks like this: 1 database, 2 tables. Message table and comment table. In the comments table, I have a common name, site, comment, etc., And I also have a unique identifier that automatically increases for each comment. Then I have a post_id that should match the assigned post.

In the message table, I have only two fields: entry_id and title. The title is manually set by me, and entry_id is automatically incremented. NOTE. The record itself is NOT stored in the database.

So my current problem is how to set post_id for each comment page and how to link entry_id to the actual post. Hope this is not too embarrassing. Thanks for any help!

-iMaster

+4
source share
4 answers

I think you should consider refactoring your code in order to store the message in your database.

From there you will have a page ( http://mysite/showpost.php?post_id=5 ) that displays your message (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 an idea of โ€‹โ€‹the data structure and code needed to accomplish what you want.

+7
source

Itโ€™s good for you to learn how to โ€œfold your ownโ€ and get exactly what you want, and learn something along the way.

You must create a comment table with a foreign key that matches the article ID.

Then, when displaying your comments, run a query to get all the comments associated with this article ID.

+1
source

You should follow Ian's recommendations and reorganize your code to use the table. Otherwise, you will have to hardcode some PHP when creating post html.

as

 $actualPostId = 1234; // you get this from the database file_put_contents ($filename, "<?php \$postID= $actualPostId;?> $rest_of_html"); 
0
source

If I read the problem correctly, I think you just need to add entry_id to the comment form as a hidden field, and when someone entry_id comment, include it as post_id , when you insert into the comment table, I think then you have the missing link between tables.

0
source

All Articles