MySQL
  Home arrow MySQL arrow Page 4 - Creating the Blog Script for a PHP/MySQL Blogging System
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Creating the Blog Script for a PHP/MySQL Blogging System
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 51
    2006-10-10


    Table of Contents:
  • Creating the Blog Script for a PHP/MySQL Blogging System
  • The Database Tables
  • index.php
  • comments.php

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating the Blog Script for a PHP/MySQL Blogging System - comments.php
    ( Page 4 of 4 )

    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
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek