Modifying Records Part 2
Senior Editor, TheScripts.com
I told you all that after we get started, it just makes sense. Now onto the third part of editing.......
elsif ($form{'action'} eq "edit_three") {
&edit_three;
}
&edit_three.... how ingenious of a name.
sub edit_three {
&header("Edit Record");
The header part of the page. We are sending it the title "Edit Record"
print qq~
<CENTER><TABLE border=1 bgcolor="#FFFFFF" cellpadding=3 cellspacing=0>
<TR bgcolor="#C0C0C0"><TD>
<CENTER>
<FORM METHOD=POST>
<INPUT TYPE="hidden" NAME="action" VALUE="edit_four">
<INPUT TYPE="hidden" NAME="key" VALUE="$form{'key'}">~;
This part was mainly just simple html, but notice the two hidden fields. The first one is our infamous action field, with a value of edit_four (guess what the next sub will be named :-) ), and the other hidden field is 'key', with the value of $form{'key'} in it. $form{'key'} is the ID number, or whatever you set $db_key to be as the record identifier.
my (%result) = &get_record($form{'key'});
Into the hash %result goes the returned value of &get_record after feeding into it the value of $form{'key'}, the database entry identifier.
Since I did not explain &get_record in the adding record section of this tutorial, I promised to explain it here.
sub get_record {
my ($exist) = 0;
Set this my variable to a null value
my ($key) = shift;
This my variable will now have the data sent to it (the database entry identifier) as its value.
open(DATA, $file);
Here we open the database file with a handle of DATA
while (<DATA>) {
As long as DATA has a value and did not reach the end of file mark yet, this loop will repeat.
(/^\s*$/) and next; # Looks for blank lines and skip them.
Just as the comment tag states, here we look for blank lines and skip them, going to the next line.
chomp $_;
$_ has a newline attached at the end, so here we ourselves of it.
@record = &grab_data($_);
Here we do our usual line processing. &grab_data splits the line into an array, @record;
%dat = &process_record(@record);
&process_record in turn changes @record to a hash for easy use.
if ($dat{$db_key} eq $key) {
$exist = 1;
last;
}
If the database link identifier is equal to the $key we are requesting, $exist is set to a non-null value, and we break out of the while loop.
}
End of While Loop
close (DATA);
Here we close our read-only copy of DATA.
$exist ? return (%dat) : return;
Here comes our new friend, the ternary operator, making yet another appearance. If $exist has a non-null value, the contents of our hash %dat is returned. Otherwise, we return no value at all.
}
End of get_record
