Firefox 3

Adding and changing records

OK, you should now be able to select records on various conditions, so let's look at something different: adding records. This feature will be used for a simple system for adding comments.

Simple actually means "very simple": each comment will be kept in a separate *.txt file, with name being a consecutive integer (1.txt, 2.txt, 3.txt...), in a given directory, while other data, like author's name, date, IP etc. will be saved to a database file called comments.csv. First let's make sure it exists, and if not, create it:

if (!file_exists('/path/to/comments.csv')) { ptb_create('comments.csv', 'G', 'id|novel|name|email|date|ip|display'); }

/path/to/ means path to a directory, where your data file is actually stored; can be empty ('L' option) or eg. $_SERVER['DOCUMENT_ROOT'] . /db/:

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/db/comments.csv')) { ptb_create('comments.csv', 'G', 'id|novel|name|email|date|ip|display'); }

In the next version of pjjTextBase, which will be out eventually, there's going to be 5th, optional parameter $forceOverwrite (default will be false), with which you won't need to explicitly check if the certain file exists, as it will be checked automatically by the script. If you want to be a beta-tester for pjjTextBase, please let me know at ptb (at) pjj dot pl.

When you get the input from a form, you'll want to 1) check its integrity (does email really look like valid e-mail address? etc.), and if everything seems to be fine, 2) write them to the database:

ptb_add('comments.csv', 'G', "$id|$novel|$name|$email|$date|$ip|1");

I assume that before you took some precautionary steps:

$name = addslashes($_POST['name']);

Now, what's up with the display field and why was is set to 1? It's just a flag saying the comment may be displayed. If later you'll come to a conclusion it's somehow inappropriate, you will be able to change the flag to 0, instead of deleting the file (let's suppose that comment in question has id = 76):

ptb_update('comments.csv', 'G', "'id' == 76", "'display' = 0");