Gossamer Forum
Home : Products : DBMan : Customization :

Posting data to DB

Quote Reply
Posting data to DB
Has anyone an idea or has this been done before.
Using JPD relational db user/items
I am attempting to record purchases from the database view lists.
How would I have a button or link from each record displayed in search results list, that when clicked would parse the relevant fields data Item and user ID (user that posted item) to another databse for recording of purchases.
Outcome would be a database of user sales of items.

Any thought on this would be appreciated

Cheers
Norvin


Quote Reply
Re: Posting data to DB In reply to
You would have to set up a separate subroutine for this. I'm afraid my brain has gone on strike at the moment. I'll see if I can get it working again and try to tackle this.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Cheers and thanks for the time, I have spent many hours on it.

Norvin
PS no rush


Quote Reply
Re: Posting data to DB In reply to
I've got a pretty good idea of how to do it. (I gave my brain a raise in salary and extra benefits. It's off strike now. Wink) But a couple of questions first.

What is your $db_key field? Is it a counter?

Where do you want the user to go after the record is added to the other database? Back to the previous search?


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
 

Hi,

Hope the head is better and thanks for the help.

The fields are set as below basically the same as demonstrated in your relations db mod.
Items.cfg
$auth_user_field is set to Userid
$db_key = ID
$db_key_track = ID

User.cfg
$auth_user_field = Userid
$db_key = Userid
$db_key_track =Userid

After the user hits the button on the listed articles for sale (to make a purchase), they would then access the add page of the purchases db with the relevant data from the seller, seller name,item/cost, displayed on the add page of the purchases db for the user then to just add his/her details, name address etc, to complete the sale, and update the purchases db. Confirmation of the sale would be acknowledge and the user would then be directed back to the search or home page of the site.
I envisage the purchases db to have the fields something like this,

item.........................from items db
cost.........................from items db
seller name...........from user db
etc and name and address fields of the purchaser.

I do hope this doesn't take up too much of your time, make the head spin too much or cause you to starve or kick the cat/dog witch ever is closest.Frown

Thanks again for the time, much appreciated.

Norvin

Quote Reply
Re: Posting data to DB In reply to
The cats are smart enough to stay away from me when I'm at the computer! Wink

My real concern was where to go after the file is written to. But it looks like I need more information about exactly what you want to be saved. And how this is going to be implemented.

You have a form where the user enters his/her name and address? And some way to connect that name and address to one or more items?


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Hi,

The purchaser having selected an item for sale form the viewed list ( from the Items DB) will then access a screen showing the sellers details from the userdb and the item for sale from the itemsdb, cost etc. Then be obliged to fill in personal details, hit the submit button to complete the sale and these details to be added to the ‘purchasedb’ , the customer would then exit via add success.

Many Sellers details have many items
(Userdb) (Itemsdb)
\ /
\ /
\ /
\ /
Items for sale
List
|
|
Customer Purchase
|
|
(Purchase DB)
Sellers details
Item details
Customer details

Cheers
Norvin

Quote Reply
Re: Posting data to DB In reply to
I really think I am dense these days.

What fields will be in the database that you want to write to? Where will they come from? (As Denzel Washington said in the movie "Philadelphia": "Explain it to me like I was a three-year-old. Smile)


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Hi,

The purchasedb would contain the following fields

1 ID.............................ID of purchase record
2 Date.........................Date of transaction
3 Selleruserid...........seller Userid from the userdb ie rec2
4 Item..........................Item that is related to the seller above from Itemdb
5 Cost.........................Cost of item from the Itemdb
6 First Name..............Purchaser
7 Second Name............... “
8 Address............................”
9 Address2. .................... ...”
10 Town...............................”
11 County/State.................”
12 Post Code......................”
13 Country...........................”

Cheers again for the time, and sorry for any confusion.

Norvin


Quote Reply
Re: Posting data to DB In reply to
No need to apologize. I'm just not sure where my brain is these days. (Maybe I should look into my astrology chart. Smile)

Let me tell it back to you and you can tell me if I'm right.

You're in the item database. There is a link to click which indicates that you want to buy a specific item. The link includes the key value for the item and sends them to the add form for the purchases database.

The add form lists information about the item and the seller and has form fields for the purchaser to enter his name and address. Have I got it so far?

You are trying to figure out how to send the key value for the item, send the purchaser to the add form and pull all of the relevant data out of the respective databases. (If this is right, I'm going to be so happy!! Laugh -- and embarrassed that I couldn't figure it out from your first post. Blush)




JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Yip that's it Laugh you have it.

Cheers
Norvin

Quote Reply
Re: Posting data to DB In reply to
Okay. (Sometimes I do make things more difficult than they are. Sorry.)

Make your link from the items database like --

<a href="$db_script_url?db=purchases&uid=$db_uid&$db_key=$rec{$db_key}&add_form=1">Buy Me</a>

Change purchases to match the name of your .cfg file for the purchase database.

Set up your purchases database with all of the fields you want to include. In this case you're going to want to keep the field names the same between all of the databases. That is, if your field name in the items database is Cost, call it Cost in the purchases .cfg file.

There's a bit of a problem with the userid. You want a userid field, but it's the userid of the seller, not the purchaser. I think the way around this would to make the change in red below to sub add_record:

Code:

unless ($db_setup eq 'purchases') {
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);
}
Again, change purchases to match the name of your .cfg file.

From there, it's just a matter of pulling in the data from the other databases.

In the html.pl file for the purchases database, after

my (%rec) = @_;

add

Code:

if ($in{'add_form') {
&switch_to_items;
%rec2=&get_record($in{$db_cols[$db_key_pos]});
$user_id = $rec2{$db_cols[$auth_user_field]};
&switch_to_user;
%rec3=&get_record($user_id);
&switch_to_purchases;
foreach $col ($db_cols) {
if (exists $rec2{$col}) { $rec{$col} = $rec2{$col}; }
elsif (exists $rec3{$col}) { $rec{$col} = $rec3{$col}; }
}
}
If it's not there already, be sure to add a $db_key_pos to your switch_to_items subroutine. Also, add a $auth_user_field to switch_to_items.

At this point, all of the values that you want to add to your purchases database are in the %rec hash, so you just need to use the regular $rec{'FieldName'} variables in your form.

I think that should do it.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Hi,

Thanks for all the help with “items user and purchase db”. I used the info you gave me and bingo, it worked fine carrying the user details across to the purchase db as needed, cheers, many thanks.


I feel to ask any more of you would be hogging your time, so when you have a dull moment and have the time and fed the cat, could you ponder how I would get a field that I have in the items db named “Sold’ settings would be “For Sale/Sold” to change over to SOLD when a purchase has been successfully made ( add success to the purchase db).

Basically how would I change (modify) a field in the items db when a record is added to Purchase db.

Has this question been asked before ??
Or would I need to go down the jave route to accomplish this.

Again thanks for all the help a neat bit of work.

Norvin

Quote Reply
Re: Posting data to DB In reply to
Don't worry about taking up my time. I've spent a lot less time with you than I have with a lot of others. Smile

So you want to change the items db when a new record is added to the purchases db.

Can be done.

Basically, it's just copying code from sub modify_record to sub add_record, with a couple of changes.

Do you have a variable in your sub switch_to_items -- $db_key_pos? If not, add a line to the subroutine

$db_key_pos = the number of your key field in the items db;

Also add the name of the key field to the subroutine:

$db_key = the name of your key field in the items db;

In sub add_record (db.cgi file), after

Code:

if ($db_key_track) {
open (ID, ">$db_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) {
flock(ID, 2) or &cgierr("unable to get exclusive lock on $db_id_file_name.\nReason: $!");
}
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock
}
add

Code:

&switch_to_items;
open (DB, "<$db_file_name") or &cgierr("error in add_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;
$found = 0;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($data[$db_key_pos] eq $in{$db_key}) {
$data[the number of your "Sold" field] = 'SOLD';
%rec = &array_to_hash(0,@data);
$output .= &join_encode(%rec);
$found = 1;
}
else { $output .= $line . "\n"; }
}
if ($found) {
open (DB, ">$db_file_name") or &cgierr("error in add_records. unable to open db file: $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 $output;
close DB; # automatically removes file lock
}
&switch_to_purchases;
Be sure to backup any data you have now when you test this. I won't guarantee that it'll work. It just seems to be the way to go. Smile

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Cheers for the fast reply, will now get down to it. Well in the morning.

Thanks again Smile The speed of your typing you might require a water cooled keyboard.
Norvin


Quote Reply
Re: Posting data to DB In reply to
Laugh I often thank my lucky stars for Mrs. Rose, my high school typing teacher. Smile

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting data to DB In reply to
Hi,

Set it up as below but cannot get the (Sold Field) to change when add success is accessed. However it will changes some of the records when a new record is added to the user db.
I also removed the &switch_to_purchases; as this affected the add a new user results.

The result I am getting once the “buy me” button is hit, shows the value on the add a record page of the purchase db as
$db_key_pos to eq 0 and the $in{$db_key} eq 9 this is the correct ID of item sold, and the $db_key_pos in the items.cfg and db &switch_to_items eq 0.

Any ideas, would I be better off creating a new sub “sold” in the db.cgi to deal with the updating so as not to affect the modifying and updating of the items db.


Cheers again

Norvin

Quote Reply
Re: Posting data to DB In reply to
I must not be understanding your database setup. (Here we go again! Smile)

Yes, it would be fine for you to move the updating routine to a subroutine which would be called when needed.


JPD
http://www.jpdeni.com/dbman/