scriptygoddess

08 Oct, 2002

Maintaining pages of content from a database

Posted by: amy In: Scripts

Several months ago, I got very, very tired of maintaining a large number of separate, standalone pages on my site. Since my pages were exactly the same except for three things (meta description tag, title tag, and content), I decided to create a database table that could hold this information and a single page that could be used to display all the information.

As far as I'm concerned, PHP and mySQL are nothing but tools to further my habit of code laziness. What you have here is the result.

This package consists of five files: add_page.php, content.php, install.php, index.php, and a readme file.

Instructions:

1) Change the database information in all four files. In addition, you will also need to provide the final location of content.php as a variable in add_page.php — so that when add_page.php displays confirmation that a page was created, a link can be made to the page (so that you can view the page immediately).

2) Open install.php in a web browser. This page installs the required table and default page in mySQL. If installation succeeds, you'll see a success message (and you may delete the install file immediately). If there are issues, post here, and include the mySQL error message you encountered.

3) Set up a folder that is protected via .htaccess. There's even a tutorial on this site somewhere. Do NOT leave add_page.php and index.php in a folder that is unprotected by .htaccess. Doing that creates a major security hole, and basically you deserve what you get if you do that. :)

4) Upload index.php and add_page.php to the .htaccess-protected folder. You should immediately be able to start adding pages to the database.

5) Upload content.php to your site. This is the file that users will load to see your content, so it needs to be publicly available (meaning, NOT in the .htaccess-protected folder). In its current form, it is very, very bare – you will want to edit it to reflect the design of the rest of your site.

That should be all. Do not delete the page named 'default' – that is what will be displayed on your site if the URL does not have a query string or the query string is invalid.

You can see it on my site here:
http://domesticat.net/content.php – the default page, listing all currently-available pages
http://domesticat.net/content.php?q=writing – a page that provides a bit of a starting point for my site

Bonuses for this way of doing things: fewer HTML pages to maintain, plus the ability to make content changes to my site through a web interface. I find this really, really useful to me when I'm traveling (which I'll be doing a lot of in the next three months) or otherwise away from the files on my home computer.

Just, please…if you're going to do something like this, be prepared to make backups of your database on a regular basis. If you don't, you risk major, ugly problems if you have some kind of database failure. Talk to your sysadmin or a knowledgeable friend about setting up automated backups if you're not comfortable doing it yourself.

The zipfile containing the pages and these instructions may be obtained at http://domesticat.net/misc/external_pages.zip (8k zip file)

(I'd also like to note that this code is quick, dirty, and not nearly as efficient as it could be. I'm just trying to get this crossed off my to-do list prior to heading north to Washington D.C. Better than not making it available at all, I think!)

10 Responses to "Maintaining pages of content from a database"

1 | Liz

October 9th, 2002 at 7:18 pm

Avatar

Hi,

I was able to connect to my database fine and install the table, but it can't add the default page, and since it could add the table, I can't undestand why. Also the index page can't seem to connect to the database either, even though it has the same db info as the install page… :/

Thanks for any help, this looks like such a great script. :)

2 | Amy

October 9th, 2002 at 10:19 pm

Avatar

ohdeargod. I did NOT leave that test bit of code in there.

If I say it enough, it'll be true.

I've uploaded a new version of the .zip file, which contains a corrected version of the install script. I forgot to change the name of the table in the 'insert default row' statement, and that's why you got the error you did.

For you, since you've done part of this already…you will need to delete the existing table from the command line.

To do this…

1) Log in using telnet/ssh.
2) Type the following, substituting what's in brackets for the real information:

mysql -u [username] -p[password] < enter > (Note no space after -p !)
use [database name]; < enter >
drop table testing_table; < enter >
exit;

That should do it. I am so, so sorry about this – just glad I got a chance to check things here prior to leaving on my trip.

If there are any other serious issues, leave me email – I will be afk and out of state until Tuesday, but will start answering mail again shortly after that.

3 | Liz

October 10th, 2002 at 5:31 pm

Avatar

Hi, maybe I didn't explain myself correctly, but I still can't connect to the database in the index.php. I created the database and the default page, but index.php cannot connect.

4 | Liz

October 13th, 2002 at 9:19 pm

Avatar

Hehe, I figured it out, its a the bottom of index.php, "domesticat" was still in the bottom db connection part. Anyway, awesome script… hehe :)

5 | Amy

October 16th, 2002 at 10:06 am

Avatar

*laugh* Just got back from my vacation (only I would call sniper-dodging in metro DC a 'vacation') and I've changed the script.

That's the worst thing about taking scripts written for me and making them available for others – it's very easy to gloss over the little things that are specific to my site.

6 | Liz

October 20th, 2002 at 5:27 pm

Avatar

I have one request for this script, could it display URLs and special characters (like a comma) corectly? It adds slashes autmatically, which seems to make sense, but these slashes seem to prevent me from writing URLs in these pages.

Is there anyway to be able to not have these slashes in the end product of the new page? Thanks
Liz

7 | Amy

October 20th, 2002 at 7:36 pm

Avatar

Ok, I'm extremely baffled by that comment, because I just wandered over to my pages and checked, and I've got everything from commas to actual PHP embedded in the pages that are being displayed by the script.

Could you provide a URL to a page that's messing up, so that I can see this for myself?

8 | Liz

October 20th, 2002 at 7:49 pm

Avatar

http://www.tricky.nu/content.php?q=testing

I don't know why, but commas are added to all the links and images. (the only part I use the script for is the main text part, side bar conent is part of another include).

9 | Liz

March 19th, 2003 at 10:07 pm

Avatar

Hi there,

I gave up, but have to decided to try this script again a few months later. Reinstalled it and everything works, except I still have the same problem:

http://www.tricky.nu/content.php?q=test

It still adds slashes in front of apostrophies… and I can't for the life of me figure out how to stop it from doing this

10 | Sara

March 27th, 2003 at 2:45 am

Avatar

Liz I'm working with Amy's template too and the only way I can get links to work in the page content field is to completely strip the double quotes after the equal sign and before the last >

I know it's not completely clean code that way, but at least it works. Don't know enough about PHP and DB's yet to say if there's a way round this other than what I've done.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements