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