Misc Links
Forum Archive
News Archive
File DB



Latest Forum Topics
wow 56 k modems are
Posted by Red Squirrel
on Oct 14 2013, 11:52:23 pm

I Need A Program
Posted by rovingcowboy
on Sep 23 2013, 5:37:59 pm

having trouble witn lan
Posted by rovingcowboy
on Sep 23 2013, 5:40:56 pm

new problem for me
Posted by rovingcowboy
on Sep 23 2013, 5:54:09 pm

RBC Royal Bank
Posted by Red Squirrel
on Aug 13 2013, 6:48:08 pm


Beginner's PHP Tutorial - Data storage
Data Management with php
By Red Squirrel

When using a file database, you need to plan ahead. You need to know how many fields you will need per file, and how many files you want. Some systems may need more files for various things. In this case, we only need one file. When planning a database, think of it as a table with rows for each field. So you want to store the name, email address, home page, and maybe a message, and to be safe and keep bad people away, the IP address.

So you would have something like this:

Nameemailhome pagemessageIP addy

From here, rows would be added for each entry. From my experience, the easiest way to do this is to have one entry per line, in the file. So a typical file database could look like this:

Ryan Auclair
Hi,<br><br>Just saying hello!<br>ttyl.
Bill Gates
Hey,<br><br>Just dropping a note to see if you received my check, if not I'll send you another one. Cash it anytime, it should not bounce.<br><br>-bill.

Take a look at a skeleton script for a guest book system. This assumes that you know the basics of php, such as nesting functions as parameters, for example: function1(function2("value")); and using GET and POST.

Woah! You are saying, but it's not that complicated. I recomend you get a text editor capable of showing the line numbers that way you are not always counting lines. I use Winsyntax.

Lines 4 to 35 only get executed if you add the ?act=insert parameter by typing it like this: guestbook.php?act=insert.

This is to insert an entry in the file database. Line 6 declares a file append stream called $writefile. Fputs() is the function to put data to a file, so you put the stream as the first parameter, in this case $writefile, and the second parameter is what you want to write to the file.

For the second parameter, what we are really doing is taking $_POST[name], which is the data entered in the form in the name field, and passing it through a few functions to fix it up.

If we put it as it is, any quotes entered in the name would appear as a slash and the quote after, so we use stripslashes() to fix it. Addslashes would do the opposite, which is needed before you insert data in the database if it may have quotes and other special characters. rtrim(removes any returns that may be at the end. Even though we add a return to the value (1 value per line) it's best to do rtrim since we are sure there's none at the end, and we add it, as sometimes it's hard to know if you should add one or not, as the case can vary.

The rest of the entries are treated the same way, but for the message field, we do something different because we allowed the user to press enter. We want to make sure the returns will appear when it is read from the database, so we turn \n's into
. For some reason putting \n does not work, you can try it, it might depend on the server platform, but I never had it working, so I put a real return instead, which is why the last entry looks funny, the two other ones simply make sure the user can't enter html. For the sake of simplicity, we did not do that to all the fields, but it's not a bad idea in a real application. (less security risks) but this is why our last entry records the IP. :)

$HTTP_SERVER[] is an array that has lot of goodies, do a google search for it to find out what they are, you can get the IP, the user agent and much more info.

On line 29 we close the file and line 31 we just display a message to go back. We could of easily just keep executing the script to display the entries, but keeping a POST data page is not a good practice to do, so we make the user click the link to go to a GET page (a normal link - not a form).

Lines 37 to 82 display the main page. We open the data stream again on line 41, but this time we use the "r" parameter because we want to read, and not append. If we wanted to write (overwrite existing files) we would use "w".

Lines 43 to 47 fetch an entry from the database. We have 5 "rows" so we have 5 variables, that's why we want every value on one line, because we read 5 lines at a time to get 5 different values, then on lines 55 to 59 we display this data, then we grab data again and continue the loop. The reason we grab data before the loop is because if the file is empty, it will skip the loop, avoiding a useless entry. Also, when we reach the end of the file while inside the loop, we will know before displaying because it will go back to the start of the loop, and realize it is over before displaying the data, avoiding a useless entry.

Again, we close the file on line 79. I hope this is starting to fall in place, lot of this is repetitious. You can actually copy the code to read and just modify it each time you need it, as it's always the same thing - just different values.

Then, the rest of the script is just html to display a form, then form action is guestbook.php?act=insert so that way it goes to the if statement at the start.

There is a big problem, however, when using a file database like this. What if you have a database of 900 entries and later on your decide you want to add another field? Maybe you want people to also put where they live. You can't just add it like that, because what will happen is that the current database will only have 5 values per entry, and now you need 6. So when you read the database, the 6th value will end up reading the 1st value of the second entry, and it will go on, mixing up all the values. You need to manually add the 6th value for each entry of the database, either put a return or a N/A after the IP. But this is time consuming.

With mysql, you can easily add a new field and value no problem, without much modification of the code and no modification of the database!

On the next page, we'll talk about mysql databases.

Next Page
24636 Hits Pages: [1] [2] [3] -1 Comments

Latest comments (newest first)
Be the first to post a comment!

Top Articles Latest Articles
- What are .bin files for? (669062 reads)
- Text searching in linux with grep (161180 reads)
- Big Brother and Ndisuio.sys (150471 reads)
- PSP User's Guide (139547 reads)
- SPFDisk (Special Fdisk) Partition Manager (117240 reads)
- How to Use MDADM Linux Raid (188 reads)
- What is Cloud Computing? (1225 reads)
- Dynamic Forum Signatures (version 2) (8769 reads)
- Successfully Hacking your iPhone or iTouch (18714 reads)
- Ultima Online Newbie Guide (35906 reads)
corner image

This site best viewed in a W3C standard browser at 800*600 or higher
Site design by Red Squirrel | Contact
© Copyright 2017 Ryan Auclair/IceTeks, All rights reserved