Home arrow MySQL arrow Page 4 - Creating the Blog Script for a PHP/MySQL Blogging System

comments.php - MySQL

In this second part of a three-part series on blogging with PHP and MySQL, we will be looking at the actual blog. The blog will be simplicity itself. I have created a style sheet that will help in making the blog look clean and neat. The blog will be based purely on a open blog system.

TABLE OF CONTENTS:
  1. Creating the Blog Script for a PHP/MySQL Blogging System
  2. The Database Tables
  3. index.php
  4. comments.php
By: Jacques Noah
Rating: starstarstarstarstar / 95
October 10, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The comments page has three main functions:

  • Display the main article.
  • Display the comments made to the article.
  • Display a form for new comments to be added.

When you click on the comments link in the index page, two numbers are sent over to the comments page: the article id and the categoryID. The comments page uses the article id to retrieve the main article and then to retrieve all the comments made to that article. Here’s the SQL that does the job:

//retrieve the main article…
if(isset($_GET['aid'])){
$_SESSION['aid']=$_GET['aid'];
$getarticle="SELECT * FROM article WHERE artid = ".$_GET['aid']."
ORDER by date_posted ASC";
if(!$result = mysql_query($getarticle)){
echo mysql_error();
}else{
$num=mysql_num_rows($result);
}
//retrieve all comments made to this article
$getcomments="SELECT * FROM article WHERE artchild = ".$_GET
['aid']." ORDER by date_posted ASC";
$getcomments_result = mysql_query($getcomments);
$comment_num=mysql_num_rows($getcomments_result);
}

Both queries are straightforward. One uses the $_GET[‘aid’] link to retrieve the main article and the other uses the same value to retrieve the comments.

The form enables a user to make comments on the main article. Here’s the SQL that handles the form data:

if(isset($_POST['theComment'])){
$query = "INSERT INTO article SET name='".$_POST
['name']."',title='".$_POST['theTitle']."',comments='".$_POST
['comment']."',";
$query .="date_posted=NOW(),categoryID='".$_POST
['CID']."',artchild='".$_POST['theID']."'";
if(!mysql_query($query)){
echo mysql_error();
}else{
$getarticle="SELECT * FROM article WHERE artid = ".$_SESSION
['aid']." ORDER by date_posted ASC";
if(!$result = mysql_query($getarticle)){
echo mysql_error();
}else{
$num=mysql_num_rows($result);
}
}
//retrieve all comments made to this article
$getcomments="SELECT * FROM article WHERE artchild = ".$_SESSION
['aid']." ORDER by date_posted ASC";
$getcomments_result = mysql_query($getcomments);
$comment_num=mysql_num_rows($getcomments_result);
}

When the form is submitted, it sends a value over to the form handler script. This value is then used in the code above. The code has two functions:

  • Insert form data into the database.
  • Retrieve the main article and comments.

The form has a couple of hidden fields that contain data, such as the title of the main article, the category ID, and so forth. All of these are used in the insert query as shown below:

if(isset($_POST['theComment'])){
$query = "INSERT INTO article SET name='".$_POST
['name']."',title='".$_POST['theTitle']."',comments='".$_POST
['comment']."',";
$query .="date_posted=NOW(),categoryID='".$_POST
['CID']."',artchild='".$_POST['theID']."'";
if(!mysql_query($query)){
echo mysql_error();

Here’s an output of the comments page:

Conclusion

That’s it for the blog part of the series. In the next part, we will create the administration side of the blog.



 
 
>>> More MySQL Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: