Solved

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

Posted on 2008-06-18
5
183 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now