yandoo Posted March 29, 2008 Share Posted March 29, 2008 Hi there, I just run into a little problem and was hoping for a little advice. I have a table in my database that has a INT AutoIncrement function as primary key .... When i insert new records thought my php page i assign the Primary key of the Record by totaling the records currently and adding 1. E.G: <?php echo $totalRows_record +1 ?> Heres the problem.... If i have say 10 records in the table and i delete record number 9 then when i try to INSERT another record i get error message DUPLICATE Primary Key! And thats because the query i am using is totaling the number of records i.e. 9 (after deleting record 9).... How can I overcome this please??? Thanks Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 29, 2008 Share Posted March 29, 2008 leave the auto-incremented value alone. Let MySQL take care of assigning the value. The record number of any record is immaterial. If you want to display records with sequential (but meaningless) numbers, just use a counter ... Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 30, 2008 Author Share Posted March 30, 2008 Hi there, How do i assign a value to the primary key? In phphmyadmin itl ets me insert a record without putting in a primary key value... But how would i do it in php????? Normally i have to have a value in hidden field as priamry key..?? Thanks Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 30, 2008 Share Posted March 30, 2008 Simple. Don't. An auto-increment does exactly that - automatically. Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 30, 2008 Author Share Posted March 30, 2008 Hi, Im confused, when i create an insert form there has to be a hidden/form value as the primary key???? Im using dreamweaver if that makes a difference??? Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 mytable ---------- id (auto_increment) firstname lastname To add new record <?php mysql_query("INSERT INTO mytable (firstname, lastname) VALUES ('John', 'Doe') "); ?> The id is incremented automatically, as it says on the tin. Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 30, 2008 Author Share Posted March 30, 2008 Hi there, Ive just test it and it works!! I always thought a primary key value was necessary! Thats ace, let auto count do it! Thank You Quote Link to comment Share on other sites More sharing options...
mwasif Posted March 30, 2008 Share Posted March 30, 2008 Hi there, Ive just test it and it works!! I always thought a primary key value was necessary! Thats ace, let auto count do it! Thank You Primary key is necessary but not the auto_increment. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.