Languages :: PHP :: Little question of PHP & Mysql |
|||
| By: PHP newbee |
Date: 03/10/2002 00:00:00 |
Points: 80 | Status: Answered Quality : Excellent |
|
I want to Insert a new record and that the field ID becomes LastRecordID+1 How can I do this? |
|||
| By: VGR | Date: 04/10/2002 00:37:00 | Type : Answer |
|
| I guess your problem was not exactly the one you wrote. I told you about the mysql-insert-id function (<A HREF="http://www.php.net/manual/en/function.mysql-insert-id.php">http://www.php.net/manual/en/function.mysql-insert-id.php</A>) As I found it unreliable, and as there can indeed be multiple inserts concurrenty, I won't recommand to use "select id from table order by id desc limit 1" which selects the mast inserted row (of course), but rather to use "select id from table where <here the fields you just inserted>"; you may check that mysql_numrows returns 1 only and take approrpiate actions if ever this is not the case. regards, |
|||
| By: PHP newbee | Date: 04/10/2002 01:37:00 | Type : Comment |
|
| But I need to know the ID just created because there are other tables that will be related by this field... How can I know? Also remember that many people will execute this at the same time... Thanks |
|||
| By: VGR | Date: 04/10/2002 18:54:00 | Type : Comment |
|
| Hello, This is automatic if you took the precaution of declaring your ID in the table as "id integer unique auto_increment" : (supposing IDs have passed the 0 value already) juste pass 0 as the ID if using syntax "values(ID,field1,field2...)" or don't specify a value if using syntax "set field1=val1, field2=val2" etc you may "upgrade" your existing database definition with "alter table modify..." That was an easy one 8-) Usually, the problem is to get the ID just created, because the function getlastinsert (or such) doesn't work well ;-) |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








