School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,443 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,527 people online right now. Registration is fast and FREE... Join Now!




Getting the ID of a newly inserted row

 

Getting the ID of a newly inserted row

xheartonfire43x

4 May, 2009 - 10:18 AM
Post #1

D.I.C Regular
***

Joined: 22 Dec, 2008
Posts: 260



Thanked: 2 times
My Contributions
I am building a call tracking system for my company and I need to give the user a Ticket ID number so that they can use it as a reference number.

Action of the form to enter in a computer problem
CODE

<cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "Task">

  <cfquery datasource="ibotix" username="root" password="ibotix$6416" name="addtick">
  INSERT INTO worklist(clientid,dateposted,description,subject)
    VALUES(<cfqueryparam value="#form.clientname#">,
                 <cfqueryparam value="#dateformat(now())#" cfsqltype="cf_sql_date">,
                 <cfqueryparam value="#form.taskinfo#" cfsqltype="cf_sql_longvarchar">,
                 <cfqueryparam value="#form.subject#">)
  </cfquery>
  <cflocation url="index.cfm?recordadded=y&amp;trackingnum=#addtick.GENERATED_KEY#">
</cfif>



I am using MySQL 4.1 and I read on the livedocs that the GENERATED_KEY variable will call back the id of the row that was inserted but I get the "Generated Key is undefined in addtick" error message. Can anyone see what I am doing wrong?

User is offlineProfile CardPM
+Quote Post


armyCoder

RE: Getting The ID Of A Newly Inserted Row

6 May, 2009 - 08:20 AM
Post #2

D.I.C Head
**

Joined: 2 Feb, 2009
Posts: 54



Thanked: 1 times
My Contributions
How about getting the max id from your table and using that?

Let's say the ID field is called trackingnum...

CODE

<cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "Task">

  <cfquery datasource="ibotix" username="root" password="ibotix$6416" name="addtick">
  INSERT INTO worklist(clientid,dateposted,description,subject)
    VALUES(<cfqueryparam value="#form.clientname#">,
                 <cfqueryparam value="#dateformat(now())#" cfsqltype="cf_sql_date">,
                 <cfqueryparam value="#form.taskinfo#" cfsqltype="cf_sql_longvarchar">,
                 <cfqueryparam value="#form.subject#">)
  </cfquery>

<!--- Get the newly generated trackingnum --->
<cfquery name"getid" datasource="ibotix">
SELECT MAX(trackingnum) AS id
FROM worklist
</cfquery>

<!--- Now go to the confirmation page and pass the trackingnum from the getid query --->
  <cflocation url="index.cfm?recordadded=y&trackingnum=#getid.id#">
</cfif>



That's how I've always done it...

Steve

User is offlineProfile CardPM
+Quote Post

xheartonfire43x

RE: Getting The ID Of A Newly Inserted Row

6 May, 2009 - 08:47 AM
Post #3

D.I.C Regular
***

Joined: 22 Dec, 2008
Posts: 260



Thanked: 2 times
My Contributions
QUOTE(armyCoder @ 6 May, 2009 - 08:20 AM) *

How about getting the max id from your table and using that?

Let's say the ID field is called trackingnum...

CODE

<cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "Task">

  <cfquery datasource="ibotix" username="root" password="ibotix$6416" name="addtick">
  INSERT INTO worklist(clientid,dateposted,description,subject)
    VALUES(<cfqueryparam value="#form.clientname#">,
                 <cfqueryparam value="#dateformat(now())#" cfsqltype="cf_sql_date">,
                 <cfqueryparam value="#form.taskinfo#" cfsqltype="cf_sql_longvarchar">,
                 <cfqueryparam value="#form.subject#">)
  </cfquery>

<!--- Get the newly generated trackingnum --->
<cfquery name"getid" datasource="ibotix">
SELECT MAX(trackingnum) AS id
FROM worklist
</cfquery>

<!--- Now go to the confirmation page and pass the trackingnum from the getid query --->
  <cflocation url="index.cfm?recordadded=y&trackingnum=#getid.id#">
</cfif>



That's how I've always done it...

Steve



There is a much better way than that that I found. Depending on what DB Engine you use there are result variables. Use the <cfquery> result attribute with it. Check out the CF LiveDocs
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:29AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month