Data Management with php
By Red Squirrel
So you would have something like this:
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:
Hi,<br><br>Just saying hello!<br>ttyl.
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.
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