Full code - news.php
<?php
mysql_connect('localhost','username','password');
mysql_select_db('db');
$query = mysql_query('SELECT * FROM news ORDER BY id DESC');
while($output = mysql_fetch_assoc($query))
{
$numberComments = mysql_num_rows(mysql_query("SELECT id FROM newscomments WHERE link =
'".$output['id']."'"));
echo $output['subject'].'<br />';
echo $output['date'].'<br />';
echo $output['news'].'<br />';
…Tutorial
In this tutorial we will add a comment system to the basic news tutorial.
mysql_connect('localhost','username','password');
mysql_select_db('db');
$query = mysql_query('SELECT * FROM news ORDER BY id DESC');
while($output = mysql_fetch_assoc($query))
{
$numberComments = mysql_num_rows(mysql_query("SELECT id FROM newscomments WHERE link = '".$output['id']."'"));
echo $output['subject'].'<br />';
echo $output['date'].'<br />';
echo $output['news'].'<br />';
echo '<a href="viewcomments.php?id='.$id.'">View Comments</a> - ['.$numberComments.']<hr />';
}
We will include the number of comments alongisde the news items. A link will take the user to a second page that will display the comments.
Full code - viewcomments.php
<?php
$id = $_GET['id'];
if(empty($id))
header('Location: news.php');
else
{
mysql_connect('localhost','username','password');
mysql_select_db('db');
$query = mysql_query("SELECT * FROM newscomments WHERE link = '$id' ORDER BY id DESC");
if (mysql_num_rows($query) == 0)
…Viewing the comments
We will use a query string to pass the id of the news item. If the id is not set then we will redirect the user to the main page.
$id = $_GET['id'];
if(empty($id))
header('Location: news.php');
If the id variable is set then then we need to check to see if any comments have been posted.
$query = mysql_query("SELECT * FROM newscomments WHERE link = '$id' ORDER BY id DESC");
if (mysql_num_rows($query) == 0)
echo 'Be the first to add a comment.';
Otherwise if we have comments then we will output them.
while($output = mysql_fetch_assoc($query))
{
echo $output['subject'].'<br />';
echo date('d-M-Y', $output['date']).'<br />';
echo $output['comment'].'<br />';
echo '<em>Posted by ~ '.$output['name'].'</em><hr />';
}
Now we need to output a form to allow a user to add a comment.
<form id="form1" name="form1" method="post" action="addnews.php?id=<?php echo $id; ?>">
<input type="text" name="name" id="name" value="Name" /><br />
<input type="text" name="subject" id="subject" value="Subject" /><br />
<textarea name="comment" id="comment">Enter your comment</textarea><br />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
Full code - addcomment.php
<?php
$id = $_GET['id'];
if(empty($id))
header('Location: news.php');
else
{
function clear($message)
{
if(!get_magic_quotes_gpc())
$message = addslashes($message);
…Adding a comment
When the user has added a comment into the form we need to enter it into the database.
$id = $_GET['id'];
if(empty($id))
header('Location: news.php');
We will again use query strings to pass the id variable, if one is not set then we will redirect the user to the main news page.
if($_POST['submit'])
{
if (empty($_POST['name']))
die('Enter a name.');
else if (empty($_POST['subject']))
die('Enter a subject.');
else if (empty($_POST['comment']))
die('Enter a comment.');
Checks are made to make sure that there are not empty fields otherwise an error message is output.
$postedby = clear($_POST['name']);
$subject = clear($_POST['subject']);
$comment = clear($_POST['comment']);
$date = mktime();
mysql_connect('localhost','username','password');
mysql_select_db('db');
if(mysql_query("INSERT INTO newscomments (id , link, name , subject , comment , date) VALUES
('', '$id', '$postedby', '$subject', '$comment', '$date')"))
echo 'Comment Entered.';
We now check the input using the clear function and enter it into the database.
Full code - deletenews.php
<html>
<head>
<title>Delete</title>
<script type="text/javascript">
function check(id){
if (confirm("Are you sure you want to delete this news item?"))
this.location.href = "?id="+id;
}</script>
</head>
<body>
…Deleting news
This is very similar to the simple news tutorial however we need one extra query to delete the comments as well as the news.
mysql_query("DELETE FROM newscomments WHERE link = $id");
MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries.