Page 1 of 1

Make the first record-id "1"

Posted: Sat Jan 24, 2009 9:16 am
by jmansa
I'm trying to get this to work:

Code: Select all

 
$result = $db->sql_query("INSERT INTO ".$prefix."_link_names (linkid,uid,catid,name,url) "
                                                       ."SELECT MAX(linkid)+1,'$uid','$category','$name','$link' FROM ".$prefix."_link_names WHERE uid=$uid");
 
The issue is the "MAX(linkid)+1".

It works like a charm if there is a record in my table with the current uid, but if the user don't have a record in the table it creates a new record but no linkid. How do I get passed this?

Re: Make the first record-id "1"

Posted: Sat Jan 24, 2009 11:20 am
by sergio-pro
Hi

There is AUTOINCREMENT field type in MySql that allows you to have unique record ids.

And your approach is potentially dangerous.
If two or more such inserts will be executed at the same time - there is a chance that two (or more) rows will be created with the same row-id

Re: Make the first record-id "1"

Posted: Sat Jan 24, 2009 12:03 pm
by s.dot
jmansa wrote:I'm trying to get this to work:

Code: Select all

 
$result = $db->sql_query("INSERT INTO ".$prefix."_link_names (linkid,uid,catid,name,url) "
                                                       ."SELECT MAX(linkid)+1,'$uid','$category','$name','$link' FROM ".$prefix."_link_names WHERE uid=$uid");
 
The issue is the "MAX(linkid)+1".

It works like a charm if there is a record in my table with the current uid, but if the user don't have a record in the table it creates a new record but no linkid. How do I get passed this?
SELECT MAX(`linkid`)+1 FROM `table`, '$uid', '$category')... etc