Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding rows then grab id 1

Status
Not open for further replies.

derwent

Programmer
May 5, 2004
428
GB
I add rows to mysql DB using and try to grab ID straight back out (ID is an auto increment, don`t try to add it in code below) however the id is coming out blank on the page. The item is entered into the DB no probs and creating the unique id.

Code:
SQL = "SELECT * FROM delreg"
set rs = server.CreateObject("ADODB.RECORDSET")
rs.open SQL,objConn, 3,3

rs.AddNew
rs("item1") = request.form("item2")
etc etc etc

bid = rs("id")
rs.close

response.write bid

Does anyone know where |I am going wrong to get the ID straight back out?

Thanks
 
You need the Last_Insert_ID

Have a look at:
or:
for older versions

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
So I would use

SELECT LAST_INSERT_ID(id) from delreg

?
 
I don't think you should pass a parameter. On the end of your insert query just add:
SELECT LAST_INSERT_ID() from delreg

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
select max(last_insert_id) from delreg may also work :)

Age is a consequence of experience
 
I have this

SQL = "SELECT * FROM delreg; select last_insert_id() as bid;"

but I`m getting the error "ODBC driver does not support the requested properties.
 
select max(last_insert_id)

does not work. the last_insert_id is returned for the last auto incremented id you used for your session. you don't need the max.

also using
select max(nameofautoincrementcolumn)
is not good practice either as you could end up selecting info after someone else has made an entry.
 
Did you follow the references I gave earlier? They give detailed examples of usage. The Select_Last_ID should be called immediately after the relevant Insert

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Thanks johnwm, I used the example to come up with the sql SELECT * FROM delreg; select last_insert_id() as bid; which brought the error "ODBC driver does not support the requested properties."

My connectrion is

Driver={MySQL ODBC 3.51 Driver};SERVER=myserver;DATABASE=mydb;USER=me;PASSWORD=mypass;OPTION=3;
 
Did you try using the 'select last_insert_id() as bid' immediately after the INSERT statement per the reference and as per my previous post? You seem to be trying to use it after a SELECT statement

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
I`m not using insert I am using addnew. This is because there are lots of fields to be inserted and find the addnew method much easier to see.
 
are you using mysql? what is addnew? is it something from a front end? the only way to get data into your database is through an import from a file or from an INSERT. irrespective if you have a tool called addnew, you still call the last_insert_id() at that point.
 
does not work. the last_insert_id is returned for the last auto incremented id you used for your session. you don't need the max.
o right sry, i thought the max function would return the highest number?

Age is a consequence of experience
 
Addnew is an ADO method on a recrodset object. I recall you have to do a select first to "prime" the column names. Some old documentation I have says if you just issue the addnew without field and values as parameters(which this code is doing , see the etc etc bit) you have to call the update method to actualy do the insert. This seems fair enough as ADO wont have a clue when your finshed doing the col=value calls. The rs.close is probabbly doing the same thing but I would do the select on last_insert_id just after the call to update. Something like.
Code:
rs.AddNew
rs("item1") = request.form("item2")
etc etc etc
rs.update
SQL2 = "SELECT last_insert_id() as lid from delreg"
set rs2 = server.CreateObject("ADODB.RECORDSET")
rs2.open SQL2,objConn, 3,3
or something close !


 
haha, thanks ingresman.

I had just done as you suggested and tested it, all worked fine. As soon as my test worked, I received the tek tips email saying you had responded. Great minds eh??

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top