Make the first record-id "1"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Make the first record-id "1"

Post 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?
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Make the first record-id "1"

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Make the first record-id "1"

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply