Solved

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

Posted on 2008-06-18
5
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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