visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Little question of PHP & Mysql


By: PHP newbee U.S.A.  Date: 03/10/2002 00:00:00  English  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 English  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 English  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 English  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

EContact
browser fav
page generated in 295.542000 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page