HomeMySQL 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.
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.