There are many ways to do this ... I am trying to give you a simple idea (without using templates or a complex method):
1) Create a database table to contain comments, I suggest the following fields:
id (integer) news_id (fk to id of the news) date (ie Timestamp) name (varchar(30... or less)) message (text)
2) On your FrontEnd page, add a form consisting of 4 fields: news-id: without explanation name: enter a text field message: textarea captcha: (to avoid bot completion), I suggest you recaptcha .
<form action="add_comment.php" method="POST"> <label for="name">Your Name:</label> <input type="text" id="name" name="name" /> <label for="name">Your Comment:</label> <textarea id="comment" name="comment"></textarea> <input type="hidden" name="news_id" value="<?php echo $news_id?>"/> <input type="submit" value="Ok" name="save"/> </form>
This form sends data to POST in add_comment.php, which must complete the following steps:
2.A) check if $ _POST data exists
if(isset($_POST["save"]))
It would be better to check the origin of the data (to be sure that it is from your site).
2.B) If $ _POST data exists, check the required fields and save the error in some structure:
if( (trim($_POST["name"]) == "") and (strlen($_POST["name"]) < 5) ){ $name_error = true; }
2.C) If there are no errors, save the data in the database: - open the db connection - collect the request. Do not forget to wrap each variable in quotation marks and run it through mysql_real_escape_string (or use prepared statements) - run this query
- redirect to current page
2.D) If there are errors, redirect to the main page with the variable in get & error = 1. Make sure that this variable is set to determine whether to print some error messages. (it is better to stay on one page and display errors, as well as fill in the data entered ( avoid xss scripts ))
3) Manage your main page by adding a script to select a comment from the database, here are a few steps:
3.A) . For each newsletter you print, you will receive an identifier (or a unique key used to store news in db).
3.B) Complete a simple selection request to get the full comment for this news:
$query = "SELECT name,message from comments where id_news = '{$_newsid}' order by date DESC";
3C) . For each comment received using this request, you can print the data as follows:
<?php foreach($query_fetched_results as $comment):?> <p class='name'><?php echo $comment['name'];?></p> <p class='comment_body'><?php echo $comment['message'];?></p> <?php endforeach;?>
4) Checking the number of comments is quite simple, perform a calculation of the data received from the request at point 3B.