I am having a problem with forcing to add a record if no record exists and have boiled it down to the following:
Some of this may not make any sense, but I tried a lot over the last 2 1/2 hours and was desperate for anything!
I printed out %rec to see what would happen. I found out that if a record existed then it would return a value of 18/32. If a record did not exist it would return a value of 1/8. I tested this with 3 id's that had records and 3 that didn't and it worked exactly like this every time.
So, why would it return a value if the record did not exist?
Here is the code I have. The code in blue I tested seperatly and it does what it is supposed to. The code in red is the problem.
In my sub html_home
if ($per_add) {
%rec = &get_record($db_userid);
if (!%rec) {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}
sub get_record {
# --------------------------------------------------------
# Given an ID as input, get_record returns a hash of the
# requested record or undefined if not found.
my ($key, $found, $line, @data, $field, $restricted);
$key = $_[0];
$found = 0;
($restricted = 1) if ($auth_modify_own and !$per_admin);
open (DB, "<$db_file_name") or &cgierr("error in get_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_; chomp ($line);
@data = &split_decode($line);
next LINE if ($restricted and ($db_userid ne $data[$auth_user_field]));
if ($data[$db_key_pos] eq $key) {
$found = 1;
for ($i = 0; $i <= $#db_cols; $i++) { # Map the array columns to a hash.
$rec{$db_cols[$i]} = $data[$i];
}
last LINE;
}
}
close DB;
$found ?
(return %rec) :
(return undef);
}
So going with what I am finding as the value of %rec I changed the sub html_home to:
if ($per_add) {
%rec = &get_record($db_userid);
if (%rec == "1/8") {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}
And what this does is forces to add if there is no record and doesn't if there is a record.
Seems like a strange workaround but it's working so far. However, I'm a little hesitant if it will work every time.
Any words of wisdom?
Thanks!
Adam
Some of this may not make any sense, but I tried a lot over the last 2 1/2 hours and was desperate for anything!
I printed out %rec to see what would happen. I found out that if a record existed then it would return a value of 18/32. If a record did not exist it would return a value of 1/8. I tested this with 3 id's that had records and 3 that didn't and it worked exactly like this every time.
So, why would it return a value if the record did not exist?
Here is the code I have. The code in blue I tested seperatly and it does what it is supposed to. The code in red is the problem.
In my sub html_home
if ($per_add) {
%rec = &get_record($db_userid);
if (!%rec) {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}
sub get_record {
# --------------------------------------------------------
# Given an ID as input, get_record returns a hash of the
# requested record or undefined if not found.
my ($key, $found, $line, @data, $field, $restricted);
$key = $_[0];
$found = 0;
($restricted = 1) if ($auth_modify_own and !$per_admin);
open (DB, "<$db_file_name") or &cgierr("error in get_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_; chomp ($line);
@data = &split_decode($line);
next LINE if ($restricted and ($db_userid ne $data[$auth_user_field]));
if ($data[$db_key_pos] eq $key) {
$found = 1;
for ($i = 0; $i <= $#db_cols; $i++) { # Map the array columns to a hash.
$rec{$db_cols[$i]} = $data[$i];
}
last LINE;
}
}
close DB;
$found ?
(return %rec) :
(return undef);
}
So going with what I am finding as the value of %rec I changed the sub html_home to:
if ($per_add) {
%rec = &get_record($db_userid);
if (%rec == "1/8") {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}
And what this does is forces to add if there is no record and doesn't if there is a record.
Seems like a strange workaround but it's working so far. However, I'm a little hesitant if it will work every time.
Any words of wisdom?
Thanks!
Adam