Gossamer Forum
Home : Products : DBMan : Customization :

Prevent Duplicate Records

Quote Reply
Prevent Duplicate Records
I am trying to prevent users from submitting duplicate records based on 4 fields within the database.

If a record exists that already contains the same "First Name", "Last Name", "Maiden Name", "Year Graduated" - the user will not be able to add the new record - but will be able to modify the existing record.

Any ideas? I tried a mod from the DBman Faq - but it didn't work.

Thanks,

Donm
Quote Reply
Re: [donm] Prevent Duplicate Records In reply to
1. Look at db.cgi at 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}) && ($data[$db_fn_pos] eq $in{'First_Name'}) && ($data[$db_ln_pos] eq $in{'Last_Name'}) && ($data[$db_mn_pos] eq $in{'Maiden_Name'}) && ($data[$db_yg_pos] eq $in{'Year_Graduated'})) {
return "duplicate key error";
}
}
close DB;

2. Add these lines in default.cfg for each of the above to correspond the appropriate line of the %db_def:

$db_fn_pos = 3;
$db_ln_pos = 4;
$db_mn_pos = 5;
$db_gy_pos = 6;


Not sure it will work like this, but it's worth a try...

Good Luck!
Quote Reply
Re: [Watts] Prevent Duplicate Records In reply to
I don't understand #2 of your post? Just where does that go in the default.cfg?
Quote Reply
Re: [donm] Prevent Duplicate Records In reply to
You could do it one of two ways... in default.cfg just list them below this part:
------------------------------
# The column name for the database key. Can be any column, but it must be unique!
# You can't have two records with the same key value!
$db_key = 'ID';
$db_fn_pos = "3";

... etc

---------------------------------------

or another way is to simply type in the field number in db.cgi like this

if (($data[$db_key_pos] eq $in{$db_key}) && ($data[3] eq $in{'First_Name'}) && ($data[4] eq $in{'Last_Name'}) && ($data[$db_mn_pos] eq $in{'Maiden_Name'}) && ($data[$db_yg_pos] eq $in{'Year_Graduated'})) {
return "duplicate key error";
}

==========================

Which ever method you use - you'll want to use this modified version instead of the one above:

Code:
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
if (($data[$db_fn_pos] eq $in{'First_Name'}) && ($data[$db_ln_pos] eq $in{'Last_Name'}) && ($data[$db_mn_pos] eq $in{'Maiden_Name'}) && ($data[$db_yg_pos] eq $in{'Year_Graduated'})) {
return "Record already exists - please modify the original";
}

I haven't tested this, so make a back-up before trying it.
Quote Reply
Re: [Watts] Prevent Duplicate Records In reply to
Watts - thanks for your help. I couldn't get the one that you posted here to work. However, I did find one at the DBMan Faq that did the job for me.

Thanks again!

Last edited by:

donm: Aug 18, 2004, 8:37 AM