Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Inserting "Auto-Increment" into a Table

Quote Reply
Inserting "Auto-Increment" into a Table
Hiya!

I am adding a mod to my site, I have a new table (called "Related") which will have related links to the subject. (For those of you who think it is strange, you have to know my site, which does NOT have Links in it, really...)

Anyway, everything is fine, I am insert using this command:

In Reply To:
$relate_db->add (
Category => $category,
URL => $url,
Date => $today,
User => $user_name,
Validate => 'No',
Site_Name => $site_name
My problem is, I am not sure how to deal with an auto incriment field. I set one up when I created the table, and then tried to just insert as above, and it did not work. Drop the auto-incriment, no problem.

I need the unique auto-incriment field; how do I deal with that?

tia!

Dave

Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Hi:

I forgot something! OK, Once I know how to deal with adding something in with auto-incement, I also need to know how to get that number for the next segment of the script, so I can use an $id variable in an e-mail I send to the admin for validation.

Hope I am not asking for too muck. I figured I can also do it by making a random number based on date+time, but that seems messy...

dave

Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
I can understand wanting related links. :) How are you handling it? I want related links for domain names, especially, for things like domain names:

AGreenerGreen.org/AGreenerGreen.com/AGreenerGreen.net, plus other "green" names, for example. I want one "primary" listing, (ususally the .com) but I want the others to be listed as "related".

So, yes, I do understand instances when relating links to links would have a use.

Another instance, more general, would be a catalog -- for instance of art supplies, or things that are related.

PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Pugdog:

Well, since my site is all Cartoon Information, it really does make sense in this instance- crazy as it sounds- to make a "baby Links" directory.

I am doing it as simply as possible. Since people will have pages for "Flintstones" (to me a Series, to a Links user, a Category) and not individual episodes (to me, a link to detailed pages) I have everything working off Category Numbers. In Related (a new Table) I track categoryNum, date, URL, Site_Name, isValidated and (hopefully) ID.

Really quick and dirty, I am afraid.

You can see an example here:

http://www.bcdb.com/...ney_Tunes/index.html

And press "Related Sites" on the left menu.
dave

Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
BTW... I forgot to answer the question.

If your def files have been resynced, and the "autoincrement" field is listed as such in that file, then if you leave the autoincrement field null, or empty, it will increment on an add.

If you have any value in it, or it's not null, it will generate an error.

PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Pugdog:

Re-Sync the .def file. Sometimes I can be so STUPID!!!!!

Yep, that worked like a charm!

Now, at the risk of overstaying my welcome... is there a quick and dirty way to get that auto-increment number returned so that the next step can use the ID number.

I looked into add.cgi, but could not see how the auto-incement number was pulled from the table and into a variable.

Does this make sense?

dave

Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Well... one of the advantages of using ID as the autoincrement field, is that add will return the ID as the success code, or undef on failure.

I've just pretty well standardized on using ID as my first field in any table I need 'record' and/or an autoincrement field in.



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Pugdog:

OK, I am not quite clear on this, and I want to make sure I have a couple points right.

First, I understand it will return the ID. OK, my auto_increment field is the FIRST field in the table, but I call it "Rel_ID". Do I need to rename it just "ID"?

Second question... Assuming I can use "Rel_ID" as the field name, will it return as "$Rel_ID", or as "$ID"?

Thanks!

Dave

dave

Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Sorry, one LAST bit of confusion!

OK, if the new number is returned, do I need to do anything special to USE that variable? Do I need to declare the variable, and what bit of code do I use to take the returned number and make it a variable?

dave

Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Hi,

The name of the column is not important, you just do:

my $new_id = $table->add ( { ... } ) or error("something went wrong: $GT::SQL::error");

and $new_id is the value of the newly inserted id.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Inserting "Auto-Increment" into a Table In reply to
Alex:

Thank you- and thanks for the other help earlier!

And thanks Pugdog!

dave