MySQL
  Home arrow MySQL arrow Page 4 - Creating the Blog Script for a PHP/MyS...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
MYSQL

Creating the Blog Script for a PHP/MySQL Blogging System
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 23
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this day and age, can't we post proper tutorials for people who want to get...
       · Just like anything else, you have to learn the basics before you refine your skills....
       · Please check the disclaimer; the whole idea of my articles is to provide a very...
       · I found the the article extremley useful as an exmaple of basic mysql and php...
     

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway