Solved

After insert pass the value from the primary key in session to the next page

Posted on 2008-06-18
5
187 Views
Last Modified: 2013-12-24
Hello experts.
I have a table with 3 columns
art_ID (primary key)
test1
test2

Now with a query:
<cfquery datasource=#dsn#>
INSERT into test1,test2
VALUES ('#form.test1#','#form.test2#')
</cfquery>
<cflocation url="Detail.cfm">.
To get the values in the detailpage i need to get the art_ID value from the that insert


0
Comment
Question by:Panos
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
adobe116 earned 500 total points
ID: 21813700
if you know you will be using Msft SQL you can just do this:

<cfquery name="insertTest" datasource=#dsn#>
INSERT into test1,test2
VALUES ('#form.test1#','#form.test2#');
SELECT @@identity as newID;
</cfquery>

Then refer to your new variable as:
#insertTest.newID#

If you are using mySQL, you can use:
SELECT last_insert_id() AS newID

If you are developing a component, I think you still have to do the old insertQry/selectQry wrapped in a cftransaction.

One more note.  You can always create a sproc in SQL that inserts the values and returns the ID or an error and your CF code can act accordingly.  That's how we do it at my company.  Check out http://www.adobe.com/devnet/coldfusion/articles/stored_procs.html (listings 3,4, and 5 for more info if you want to go down that path)
0
 
LVL 2

Expert Comment

by:adobe116
ID: 21813733
sorry, when i said "developing a component," I meant a component that other developers could use... something you would post for download for instance that has to be DB independent.
0
 
LVL 2

Author Comment

by:Panos
ID: 21813979
Is this working in MySQL too.
Can i make the insert, and on the same page get the art_id that was generated and than pass this with a session to the next page?
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 31468310
Thank you for your help.
regards
Panos
0
 
LVL 2

Expert Comment

by:adobe116
ID: 21814893
the full code for mySQL would be (note the last line of the cfquery):

<cfquery name="insertTest" datasource=#dsn#>
INSERT into test1,test2
VALUES ('#form.test1#','#form.test2#');
SELECT last_insert_id() AS newID
</cfquery>

then you would do:
<cfset session.variableName = insertTest.newID />
if you wanted it in a session.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question