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 Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/ 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 ... Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504302 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 Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504383 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. Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504385 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 Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504387 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. Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504390 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 Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504398 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. Link to comment https://forums.phpfreaks.com/topic/98536-duplicate-id/#findComment-504775 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.