Adding Records Part 2
Senior Editor, TheScripts.com
Now, as that sea of if/elsif/else statements mentions above, any submitted data from sub add_record is forwarded to sub add_record_two, seen below;
sub add_record_two {
$form{$db_key} = &get_next_id();
&get_next_id is a sub created to look at the file declared in $numberfile, increment the number, and return it. Here it is setting the key for the database to that number.
my ($line) = &make_data(%form);
&make_data formats the data as needed for the database, using the delimiter. The %form hash being sent to it contains all of the data just submitted. The value returned is the line to be appended to the data file. Lets explain this sub before we move on though....
sub make_data {
my %record = @_;
The hash %record is now a my variable, and is filled with the hash we just sent to it.
my ($rec, $line) = "";
These two variables are just declared as my variables on the spot, so we dont have to do them later. They also now have null values.
foreach $field (@db_fields) {
# repeats for
#all of your configured fields
Since @db_fields contains all of the database fields, we re-iterate over it to encode all of the necessary data.
$rec = $record{$field};
$rec is set to the value of $record{$field}, which is repeated for each database field.
$rec =~ s/\r//g;
# Scrap that Windows Line Feed
Windows has a funny line feed accompanying new lines, so we will just get rid of it.
$rec =~ s/\Q$delimeter\E/~~/og;
# Scraps the delimeter if used, and makes it ~~
If someone uses the delimeter in their entry, it could mess stuff up, so we encode it here. The two regular expression parameters beng used are g, which means globally, and o, which just compiles the expression once to avoid funky little errors and annoyances.
$rec =~ s/\n/``/g;
# Grabs Newlines, and makes them ``
Since we can't actually print the newlines in the database, as each record is on its own single line, we encode it.
$line .= $rec.$delimeter;
# Your Record is Being Made
The Record is being attached to the delimeter, which is being appended to the end of the $line variable.
} Whoops... gotta scrap that delimiter at the end (extra one). Chop just cuts off the last character.
chop $line;
return $line."\n";
Here we return the new record, with the nice shiny line feed at the end.
}
End of sub make_data Back to our add_record_two sub.......
open (DATABASE, ">>".$file);
if ($flock) {
flock(DATABASE, 2)
}
If your system supports flock, use it! flock will prevent your files from messing up with more then one user modifying them at once.
print DATABASE $line;
close (DATABASE);
Here the newly created $line variable is appended to the datafile, and it is closed, also releasing the flock if it is created.
Here is that header function being used again, notice the title being sent with it?
&header("Add a Record Successful");
print qq~<CENTER>Here Is Your Record</CENTER><p>~;
my (%result) = get_record($form{$db_key});
I will be explaining the sub get_record later (in the edit section, near the end of the tutorial), but, this is pretty much some simple error checking to make sure the database entry was actually added.
if (%result) {
&build_record_page(%result);
}
Here the error checking continues. Notice how &build_record_page is now having data sent to it? Well, if you remember the contents of that sub, it will now be showing the values in its form fields, instead of leaving them blank. This is to check to see if your data is entered properly.
else {
print "Error - No Record Added";
}
&footer;
}
Phew, thats done. Now on to one of the most important functions a database can ever have. The search function. Just a note, this is indeed the most complicated part of the whole script, but afterwards, its all downhill from that point. Enough of this babbling, time to get to work.
