Solved

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

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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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