Using Php and Mysql to Develop a Cms

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.

Watch the video related to Php Scripts

Help answer the question about Php Scripts

I want to add a form to my site, to collect some data and an image. Any free php scripts?
It's a simple form, and i need users to upload an image.
I need a form generator…
anyone?

About Author

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.

18 Responses to “Using Php and Mysql to Develop a Cms”

  1. monkeymanbob says:

    Nice work, you did pretty good.

  2. champ0y says:

    You’re really good man. You’ve got excellent talent.

  3. TheTroubadourMusic says:

    :O

    :O

    :O

    how is this not a real photo?

  4. relientkfan153 says:

    The "something" can be accessed very easily with PHP. If you have a web page address that looks like this:

    index.php? month=september&name=smith&age=24

    Then you can access those values in PHP like this:

    <?php
    echo $_GET('month');
    echo $_GET('name');
    echo $_GET('age');
    ?>

    This will print septembersmith24.

    As for accessing a text file, that's easy too:

    <?php
    $text = file_get_contents( 'textfile.txt' );
    echo $text;
    ?>

    This will read the entire contents of the file textfile.txt into the variable $text, and then print it out for you.

    You can also access many databases with PHP, I highly recommend you use MySQL. It's a bit too complicated to describe here, though. There are some good references made to good books by folks above. The "Teach Yourself" series is great.

  5. Atticus says:

    You can use a menu maker if you're not sure how to code something on your own. There are a gazillion of them out there and many are even free. Pick one that suits you…

    http://www.google.com/search?source=ig&hl=en&rlz=&q=web+menu+maker&btnG=Google+Search

  6. Simplicated says:

    If you have sensitive files that should be accessible only by scripts, put them somewhere on the server other than the web root. Your scripts will still be able to access them, but nobody can pull them off the web.

    /
    – /htdocs
    – — /www.site.com
    – /securedocs

  7. Sameer A says:

    http://www.elite.ro/free-hosting-php/
    http://www.awardspace.com/
    http://www.free-phphosts.com/
    http://www.freehostia.com/free_hosting.html

    Really what you need to run php is some one who also offers a sql database these do but I dont get the website builder thing?
    if your messing around with php why would you need a WYSIWYG builder?

  8. lidiabarbarita says:

    Very nice!!

  9. Forbidia says:

    Brilliant Willy, Just Brilliant =D

  10. Ars Magicana Arcanum says:

    Windows XP PRO has functionality for running a web server built in

    IIS is an option, however, under a "Default" installation of XP Pro, it might not be installed

    Control panel – > add / remove programs -> Add Remove Windows COmponents – Check "Internet Information Services"

    You could also look @ apache as has been mentioned, however, IIS is PART of windows.

  11. avb17018411 says:

    woww that’s really relax and beatiful soung .good picture of jhony depp !

  12. virgocrabtiger373 says:

    http://www.phpfreaks.com/tutorial_cat/8/Basics-&-Beginner-Tutorials.php

  13. Mike says:

    Hi,

    Best Site to Learn PHP http://www.w3schools.com/php/default.asp

    I know this Answer Will solve your problem. Do one thing visit http://www.zakhas.com/Forum be a member first and Download 1800 Industry standard PHP Projects and source codes. Directly without any interrupting. Follow below link for direct access to the download link.http://www.zakhas.com/Forum/ViewThread.asp?Thread=161&Forum=26
    Definitly it will solve your problem. Download it BE Quick.

    Or you can try this sites below

    http://www.planetsourcecodes.com

    or

    http://www.scriptswave.com/free/scripts/php_scripts/9.html

    If you find this answer is best then you can choose it as best answer.

  14. Faithless863 says:

    hm i couldn’t tell the difference between photograph and painting comparing the final resault.

    This is sick

  15. superchode20164 says:

    amazing! Willy teach me how to paint like you!

  16. warah110 says:

    Perfect.

  17. Jeremy H says:

    I don't think there is such a thing as a PHP emulator, and I don't know why you'd need one. It's likely that you either didn't install PHP and apache correctly or you aren't using it right.

    Use XAMPP to install PHP and apache. It will also include MySQL and phpMyAdmin (which you will need sooner or later) and some other good stuff too. It's much easier than trying to configure PHP and apache by hand. Best of all, it's all configured to run together immediately. XAMPP and all of its components are free and open source.
    http://www.apachefriends.org/en/xampp.html

    Once you've got a web server, you need to turn it on (that's not automatic.) There's a nice control panel that makes this easy with XAMPP. PHP files will only work if they're in a specific subdirectory (usually htdocs in the apache directory structure) Also, you can't just load a PHP file into the browser the same way you do an HTML file. Instead, you need to point the browser to
    http://localhost/yourFileName.php

    PHP is called by the server, so if you use a mechanism (like the file:// directive) that bypasses the server, your PHP programs won't run.

  18. ampersand1 says:

    http://www.phpfreaks.com

    If you are ready invest: Get Larry Ullman: 'PHP …" Nice book

Leave a Reply