Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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