Gossamer Forum
Home : Products : DBMan : Customization :

Duplicate Key Error

Quote Reply
Duplicate Key Error
Hi! I've got a quick question. This is what I need to do...

The database is setup with the UserId Of the person as the db_key. This allows the user to only have one record in the database, and is setup this way for several other reasons. This works great for the users of our database that have web access, and can signup and enter the information - but also creates the following problem.

When we have a member that does not have internet access - it is up to an admin to enter this record. BUT - since the admin is adding the record, it adds the ADMIN's user id as the db_key - creating a duplicate key error. I need to have some way that an admin can create a record, and have it maybe ammend his userid with a either sequential number (similar to the way db_key_track increments a number in a file). This would allow any admin have their own record (with their plain userid), and also add another record (with a key/userid of admin001) or something like that.

I have snipped the validate coding from db.cgi, and have placed it below. Is there anyway to code it like "if ($data[$db_key_pos] eq $in{$db_key}) then $db_key = $user <--(Somehow have to put a sequential or random number here)



Code:

In sub validate_record:

if ($in{'add_record'}) { # don't need to worry about duplicate key if modifying
open (DB, "<$db_file_name") or &cgierr("error in validate_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);
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
}
close DB;
Thanks,
Chris Kinsler

Quote Reply
Re: Duplicate Key Error In reply to
Here's a idea:

In sub html_record_form, you could have two different input boxes dependent of the person adding / modifying the record. Eg:

print qq|<input type="hidden" name="userid" value="$rec{'userid'}">| if (!$per_admin);
print qq|<input type="text" name="userid" value="$rec{'userid'}" size="10">| if ($per_admin);


This way, it will automatically add the current users id, but if the person is admin, they will have an opportunity to change it.

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/