![]() ![]() The next line creates the publicationDate field, which stores the date that each article was published. We’ll use this unique value as a handle to refer to the article that we want to display or edit in the CMS. So the first article will have an id of 1, the second will have an id of 2, and so on. We also add the auto_increment attribute, which tells MySQL to assign a new, unique value to an article’s id field when the article record is created. We also specify the NOT NULL attribute, which means the field can’t be empty (null) - this makes life easier for us. This lets our CMS hold up to 65,535 articles. This has a smallint unsigned (unsigned small integer) data type, which means it can hold whole numbers from 0 to 65,535. Each field holds a specific type of information about each article.įirst, we create an id field. A table consists of a number of fields (also called columns). We’re now ready to define our table structure. The stuff inside the parentheses defines the structure of the data within the table, explained below… We do this because we can’t define a table with the same name as an existing table.ĬREATE TABLE articles ( ) creates the new articles table. Let’s break the above code down a little:ĭROP TABLE IF EXISTS articles removes any existing articles table (and data - be careful!) if it already exists. It’s written in SQL, the language used to create and manipulate databases in MySQL (and most other database systems). The above code defines the schema for the articles table. Summary text NOT NULL, # A short summary of the articleĬontent mediumtext NOT NULL, # The HTML content of the article Title varchar(255) NOT NULL, # Full title of the article PublicationDate date NOT NULL, # When the article was published Id smallint unsigned NOT NULL auto_increment, A table’s schema describes the types of data that the table can hold, as well as other information about the table.Ĭreate a text file called tables.sql somewhere on your hard drive. This, as you’d imagine, holds all of the articles in the system. Our simple CMS has just one database table: articles. Step 2: Create the articles database table If you’re not sure what to do on your server, ask your tech support team for help. Some web server setups let you create databases via a web-based tool such as cPanel or Plesk (in fact sometimes this is the only way to create MySQL databases). That’s it! You’ve now created a new, empty database, into which you can put your database tables and content. If you’re working on a development server, such as your own computer, then you can use the root user for this, to save having to create a new user. username should be a user that has permission to create databases. Then enter your MySQL password when prompted. Open a terminal window and enter the following: The first thing we need to do is create a MySQL database to store our content. Ready? Grab a cup of tea, and let’s get coding! Step 1: Create the database If you don’t want to create the files yourself, simply download the finished zip file, which contains all the code files and folders. This page contains all the code for the CMS, ready for you to copy and paste into your own files. Here are the steps we’ll need to follow to create our CMS: The “view article” page, letting visitors see a single articleĮach article will have an associated headline, summary, and publication date.The article listing page, listing all articles.The homepage, listing the 5 most recent articles.The CMS will have the following features: Our first job is to work out exactly what we want our CMS to do. Setting all this up is beyond the scope of the tutorial, but a really easy way to do it is simply to install XAMPP on your computer. For this tutorial, you’ll need to have the Apache web server with PHP installed, as well as the MySQL database server running on your computer. (For security reasons this demo is read-only, so you can’t add, change or delete articles.) You can also click the Download Code link above to download the complete PHP code for the CMS, so you can run it on your own server. Yes, it can be done!Īlong the way, you’ll learn how to create MySQL databases and tables how to work with PHP objects, constants, includes, sessions, and other features how to separate business logic from presentation how to make your PHP code more secure, and much more!īefore you begin, check out the finished product by clicking the View Demo link above. In this tutorial I’ll show you how to build a basic, but fully functional, CMS from scratch in just a few hours. : This article and the code were updated for PHP7 compatibility.īuilding a content management system can seem like a daunting task to the novice PHP developer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |