Using PHP and MySQL to Develop a Simple CMS – Version 1

https://gulase.com/wp-content/uploads/2021/10/using-php-and.jpg

In this article Ill try to describe how to develop a very simple Content Management System (CMS). Ive 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 wont use any time describing CMSs, what they are, or why you should or should not use them as there are plenty of excellent articles on this site that describe them perfectly well. Ill just explain one way of developing one.

This CMS consists of a single web page (index.php) that can have its contents updated by use of a standard form (updatePage.htm). 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. In later articles Ill look at various ways to extend the CMS to make it more useful.

There are four files in this project:

cms.sql updatePage.htm 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 initial data into the table. You only need to use this file once.

updatePage.htm
This web page contains a simple form that can be used to go into the contents displayed by index.php.

updatePage.php
This is the form handler – the script that processes the data (entered in updatePage.htm) 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. dominant meaningful (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 later 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 additional to the table. As we only have one web page (index.php) in our imaginary website, we will only have one record and consequently one integer: 1. If we additional 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 dominant meaningful, which you can think of as a reference for the table. As we only have one table, which will contain only one record, we wont make any use of the meaningful. Ive included it though because its good practice to do so.

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

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

updatePage.htm

(observe that for characterize considerations, Ive inserted spaces into the HTML tag names, otherwise they would be processed as HTML code.)

1.
2.
3. Really Simple CMS
4.
5.
6. Really Simple CMS
7.
8. go into page content:

9.
10.
11.
12.

This is just standard HTML, which probably doesnt really need explaining. All it does is present a form, the contents of which are sent to updatePage.php when the Update Page button is clicked.

updatePage.php

1.

This is the form handler, thats to say, the script that processes the data entered into the form (in updatePage.htm).

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 great number name, which Ive assumed to be localhost, the database user, which Ive assumed to be root, and the password needed to connect to the database. I have no idea what this would be for your system so Ive just written the information 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.
2.
3. Home Page
4.
5. Home Page
6.
14.
15.

This is the web page that displays the contents from the database. Its called index.php instead of index.htm because the web page contains PHP code. If the page was called index.htm, the PHP preprocessor, which is part of the web server, would not know that the page contained PHP code, and would consequently not try to course of action the script part of the page (lines 6 to 13). This would cause the script itself to be displayed in the browser instead of the HTML generated by the script.

Most of the lines in this web page are pretty straight forward and dont 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 sustain PHP and MySQL; if it doesnt, the CMS wont 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 its 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 youve set up the database and table, you can simply browse to the updatePage.htm web page and update the database contents. You can then browse to the index.php page to view the updates.

leave your comment

Featured Posts

Recent Posts

  • 350 T15 An Phú Đông Q.12 TP.HCM
2,750.00$ (Fixed)
  • 350 T15 An Phú Đông Q.12 TP.HCM
9.98$ (Fixed)
  • Tĩnh lộ 8, CỦ CHI
5,400,000.00$ (Negotiable)
  • Thạnh Xuân 38, Phường Thạnh Xu...
108,000.00$ (Negotiable)

Recent comments

    Top