Remember Me
forgot your password?

Using Php and Mysql to Develop a Cms

In this article I'll try to describe how to develop a very simple Content Management System (CMS). I've chosen PHP as the server-side scripting language and MySQL as the database management system purely because I think they are fairly easy to use and they do the job very well.

I won't spend any time describing CMSs, what they are, or why you should or should not use them as there are plenty of excellent articles around that describe them perfectly well. I'll just explain one way of developing a CMS.

This CMS consists of a single web page (index.php) that can have its contents updated by use of a form (editPage.php). The contents entered via the form are stored in a database, and are accessed and displayed by the web page. Although this CMS is too simple to be of any real use, it could be used as the starting point for a real life CMS solution.

There are four files in this project:

cms.sql
editPage.php
updatePage.php
index.php

cms.sql
This file creates a database called cms, and creates a table in that database called page. It also loads some intial data into the table. You only need to use this file once.

editPage.php
This web page contains a simple form that can be used to enter (and edit) the contents displayed by index.php.

updatePage.php
This is the form handler - the script that processes the data (entered in editPage.php) and inserts it into the database table (page).

index.php
This is the web page that displays the data held in the database table.

cms.sql

1. CREATE DATABASE cms;
2. USE cms;
3. CREATE table page (
4. pageID integer auto_increment,
5. contents text,
6. primary key (pageID)
7. );
8. insert into page (pageID, contents) values ('1', 'dummy text');

Line 1 creates a database called cms in the MySQL database management system.

Line 2 tells MySQL to use the database for the subsequent commands.

Line 3 creates a table in the database.

Line 4 creates a column called pageID, which will contain integers, and which will be automatically incremented as new records are added to the table. As we only have one web page (index.php) in our imaginary website, we will only have one record and therefore one integer: 1. If we added additional pages to the table, they would be automatically numbered (2, 3, 4, etc).

Line 5 creates a second column called contents, which will contain text. This is where the editable contents displayed by index.php will be stored.

Line 6 sets pageID as the primary key, which you can think of as a reference for the table. As we only have one table, which will contain only one record, we won't make any use of the key. I've included it though because it's good practice to do so.

Line 7 simply closes the bit of code that was started in line 3.

Line 8 inserts some intial data into the table: 1 as the first (and only) pageID, and 'dummy text' as the contents of the first record.

editPage.php

(Note that for display considerations, I've used square brackets '[' instead of angle brackets for tag names.)

1. [html]
2. [head]
3. [title]Really Simple CMS[/title]
4. [/head]
5. [body]
6. [h1]Really Simple CMS[/h1]
7. [?php
8. mysql_connect("localhost", "root", "password");
9. $result = @mysql_query("SELECT contents from cms.page");
10. while ($row = mysql_fetch_assoc($result)){
11. $contents = $row['contents']; // Do not change these to angle brackets
12. }
13. ?]
14. [form name="form1" method="post" action="updatePage.php"]
15. Enter page content:[br][textarea rows="10" cols="60" name="contents"][?php echo "$contents" ?][/textarea]
16. [input type="submit" name="Submit" value="Update Page"]
17. [/form]
18. [/body]
19. [/html]

Most of this file is fairly simple HTML that doesn't need explaining. However, the following bits of code are probably worth discussing.

Lines 7 through to 13 contain PHP code to connect to the database and extract the contents of the web page.

Line 15 contains a tiny bit of PHP code to display the contents in the form's textarea. This line shows how easy it is to integrate bits of PHP code into lines of HTML code.

Remember though that in order to use PHP code in an HTML page, the file has to have an extension of .php. If it does not, the PHP code will not be processed by the web server.

updatePage.php

1. [?php
2. $contents=$_REQUEST['contents']; // Do not change to angle brackets
3. mysql_connect("localhost", "root", "password");
4. $result = @mysql_query("UPDATE cms.page SET contents='$contents'");
5. mysql_close();
6. ?]

This is the form handler, that's to say, the script that processes the data entered into the form (in editPage.php).

Line 1 signifies the start of a PHP script.

Line 2 requests the contents that were posted from the form. We could have written
$contents=$_POST['contents']; instead if we had wanted to.

Line 3 connects to the MySQL database server, setting up the host name, which I've assumed to be localhost, the database user, which I've assumed to be root, and the password needed to connect to the database. Naturally, I have no idea what this would be for your system so I've just written the word password.

Line 4 updates the page table in the cms database with the new contents.

Line 5 closes the database connection.

Line 6 closes the PHP script.

index.php

1. [html]
2. [head]
3. [title]Home Page[/title]
4. [body]
5. [h1]Home Page[/h1]
6. [?php
7. mysql_connect("localhost", "root", "password");
8. $result = mysql_query("select contents from cms.page");
9. while ($row = mysql_fetch_assoc($result)){
10. $contents = $row['contents']; // Do not change to angle brackets
11. }
12. echo $contents;
13. ?]
14. [/body]
15. [/html]

This is the web page that displays the contents from the database.

Most of the lines in this web page are pretty straight forward and don't need explaining. Lines 6 to 13 contain the PHP script that extracts the contents from the database and displays (echos) it in the browser.

Installing/Running the CMS

To use the CMS you need to copy the files onto your web server into the area allocated for web pages. Your web server needs to support PHP and MySQL; if it doesn't, the CMS won't work.

You also need to use the correct database connection names and passwords (those used in the mysql_connect lines in the PHP scripts).

Exactly how you run the cms.sql file to set up the database and database table will vary from web server to web server so it's difficult to give precise instructions here. If you have a phpMyAdmin icon or something similar in your web servers control/administration panel you should be able to use that.

Once you've set up the database and table, you can simply browse to the editPage.php web page and update the database contents. You can then browse to the index.php page to view the updates.

John Dixon

John Dixon is a web developer working through his own company John Dixon Technology. As well as providing web development services, John's company also provides free open source accounting software written in PHP and MySQL.

Rate this Article: 2.7 / 5 stars - 3 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Web Design Articles
  • More from John Dixon

Ecommerce Website Development for Online Business Making

By: Arun Kumar | 16/11/2009
What comes to your mind when you think of ecommerce? Doesn’t it bring to you an image of a platform on what you can easily make all your online transactions or do all your purchases instantly? Yes, it’s ecommerce that that has really made it easy for us buy, sell and avail products and services at lightning speed completely online.

Web Design & Development Services India are effective and affordable

By: Shipra Kaul | 16/11/2009
The widespread expansion of corporate structures and the Internet modernization have made us to think about sustainable and high quality technology, development of web structures, e-commerce websites which are user-friendly and marketing of Internet at affordable prices.

Website Design Company - How to go for the right one

By: Amya Adora | 16/11/2009
A website Design Company is aimed at designing the website and at developing it in accordance with the latest online trends. The main purpose of this company is to make organization's website search engine friendly, aesthetically appealing and user friendly.

What Makes a Great Home Page - The Ten Top Tips

By: Patricia Davidson | 15/11/2009
You'd be amazed at how little time you have to grab your visitors' attention when they hit your home page. Here are the top ten tips to make them want to know more.

How to stay ahead of find your competitors keywords With a Agile testing approach Website

By: Joseph Freeman | 15/11/2009
Many content management systems and website builders say see the demo and when you click the link for that coveted demo, you are presented either with tutorial video with one of those confusing faces or with form for filling where they want to you tell everything about your.

How to Hire Web Design Firms

By: Va Infotech | 15/11/2009
Whether you're just starting your company or you've been in business for many years, chances are that you'll eventually have to invest in the services of a website design company. This isn't a task for the faint of heart because while there are some phenomenal companies out there, but there are also some very poor ones. Picking the right one can put your company on the fast track to Internet success, but choosing the wrong one can leave you with nothing to show for your time, money and frustrati

How to Build a Website? Ten Things You will Need to Build a Successful Website

By: Nicole McCullum | 14/11/2009
So you have decided that you absolutely need a website. Before you dive in, I would like to share with you ten things you would need to know on how to build a website, in order to ensure the success of your website project.

The value of IT outsourcing

By: Brigette | 14/11/2009
IT is an integral part of any business these days. It is simply impossible to run a successful business without a good IT infrastructure in place. You may think that because you are a smaller business, you can slip under the IT radar until you get bigger.

Using Php to Populate a Drop Down List Box From a Mysql Database Table

By: John Dixon | 05/09/2008 | Web Design
Drop down list boxes provide a great way to enable visitors to your web site to select an item on a form. Normally, you hard code the items on the drop down list box - but what about if you want to get the items from a database table.

Finding Hidden Characters in a File

By: John Dixon | 27/06/2008 | Programming
It is sometimes necessary to find hidden characters within one or more files.

Web Site Promotion Tips

By: John Dixon | 20/06/2008 | SEO
When trying to get to the top of the search engine rankings there are certain things you should do, and other things you should not, in order to increase your chances of getting a top ten placement.

Exploiting Google Adsense

By: John Dixon | 08/04/2008 | Internet Marketing
Google Adsense provides a great way to generate revenue from a web site.

Using Perl and Regular Expressions to Process Html Files - Part 2

By: John Dixon | 17/03/2008 | Programming
In Part 1 we looked at what Perl and regular expressions are, and discussed how to use them to process ASCII files such as HTML files. In this part we'll develop a Perl script to process an HTML file.

Using Perl and Regular Expressions to Process Html Files - Part 1

By: John Dixon | 17/03/2008 | Programming
Like many web content authors, over the past few years I've had many occasions when I've needed to clean up a bunch of HTML files that have been generated by a word processor or publishing package. Initially, I used to clean up the files manually, opening each one in turn, and making the same set of updates to each one. This works fine when you only have a few files to fix, but when you have hundreds or even thousands to do, you can very quickly be looking at weeks or even months of work.

Size Really Does Matter

By: John Dixon | 14/03/2008 | SEO
I believe that by following three basic rules, it is relatively easy to achieve a high ranking with the major search engines: 1. Add lots of relevant content; 2. Build up plenty of good quality inbound links; 3. Be patient.

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.18, 1, w2)