Gossamer Forum
Home : Products : DBMan : Customization :

adding to two databases

Quote Reply
adding to two databases
I have an application that contains a main database and a guest database. When an individual creates a new record in the main database, I would like to create an entry in the guest database and import some information that was just entered in the main database.

I assume I should do this within the add_success mod, but I'm not sure how. Any help would be greatly appreciated.
Quote Reply
Re: adding to two databases In reply to
Yes, you could add this to the html_add_success subroutine.

It probably would be best to add the code at the very end of the subroutine, just before the final }.

First, get all of the information for the current record into the %rec hash:

Code:
%rec = &get_record($in{$db_key});

Next, you're going to need to build an array of the fields in the "guest" database, since you aren't sending all of the information. Be sure to include the $db_key field from the main database, and make that the key field in the guest database. That way you can be pretty sure you won't have a duplicate key in the guest db.

Code:
@db_cols = ('Field1','Field2','Field3');

Then define the name of your guest database.

Code:
$db_file_name = $db_script_path . "/guest.db";

Finally, add the record to the database.

Code:
open (DB, ">>$db_file_name") or
&cgierr("error in add_record. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2)
or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB &join_encode(%rec);
close DB;





------------------
JPD





Quote Reply
Re: adding to two databases In reply to
While it updates the database fine ... except for one small problem:

my db_key in the main database is unique with itself, but not within guest (since add and deletes are possible as well to guest. So is there a way to ensure that the db_key used is the next record within guest?

Also, I'm trying to use data from a search of guest database as input into an add. Any ideas how I can accomplish this

Quote Reply
Re: adding to two databases In reply to
I don't understand why there would be a problem with using the same key field in both databases. From what you said, one record in the main database would correspond to one record in the guest database. If you are using a counter for the main database ($db_key_track = 1), then there wouldn't be a problem. But maybe I don't understand what you need.

I don't know what you mean by using "data from a search of guest database as input into an add." Can you give me an example?

------------------
JPD





Quote Reply
Re: adding to two databases In reply to
Well I found what I think is the problem, when I looked at the db file I found the ID was not in sequence for the first record. I move it into the right position and every thing is working fine. Thanks for your help