Home arrow PHP arrow Page 3 - Defining the Core Structure of a PHP Blogger

Defining the insertBlog() method - PHP

In the first part of this three-part series, we'll examine the basic outlines of creating a blog application with PHP 5. If you're currently working with PHP 4, this program can be adapted to that version of the language.

TABLE OF CONTENTS:
  1. Defining the Core Structure of a PHP Blogger
  2. Defining the blogger's core structure
  3. Defining the insertBlog() method
  4. Defining the updateBlog() method
  5. Defining the deleteBlog() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
November 21, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I proceed to define the signature of the method responsible for adding new entries to the blog application, let me first explain briefly how I'm going to create the corresponding database schema with MySQL. In simple terms, I'm going to use only one table, called "blogs" (yep, my own creativity sometimes blows me away), which will contain the following fields: "ID," "title," "author," "content" and finally a column named "date."

As you'll imagine, each one of the referenced fields will house the ID of a particular blog entry, its title and date, the name of the author who posted it and lastly the content in question. Definitely, after having at hand the description of all the corresponding fields that comprise the "blogs" database table, creating its structure is reduced to something as simple as this:

CREATE TABLE blogs (
            id INTERGER(4) UNSIGNED AUTO_INCREMENT, PRIMARY KEY,
            author VARCHAR(45) NOT NULL,
            title VARCHAR(45) NOT NULL,
            content LONGTEXT NOT NULL,
            date TIMESTAMP
)

All right, now that you know how the previously-defined "blogs" database table has been properly defined, have a look at the following method that belongs to the "BlogProcessor" class. It is called "insertBlog()" and is tasked with adding new entries to the referenced table:

// insert new blog
private function insertBlog(){
$title=$this->blogData['title'];
            $author=$this->blogData['author'];
            $content=$this->blogData['content'];
            $this->mysql->query("INSERT INTO blogs (id,author,title,content,date)
VALUES (NULL,'$author','$title','$content',TIMESTAMP(10))");
            header('Location:'.$_SERVER['PHP_SELF']);                            
}

As you'll certainly agree, the above method is quite straightforward. It merely inserts a new blog entry into the respective "blogs" database table, and uses the values entered on the corresponding web form to perform the insertion process. Finally, the method finishes its execution by redirecting the user to the same web document where all the blogs will eventually be displayed. Simple and efficient, right?

At this stage, and assuming that the method you saw before isn't difficult to understand, let's move forward and see together how to define another method included with the "BlogProcessor" class, which will be useful for updating a particular blog entry.

As usual, to see how this will be done, please click on the link below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: