logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 05-12-2010, 11:56 AM   #1
Kan20
 
Join Date: Jun 2009
Posts: 11
Default PHP code for auto increment

Hi,

I'm using Wamp server on Windows XP machine.

MySQL's auto increment field doesn't reset wen I delete all the entries in the table. It continues with last inserted ID.

I want it reset or start with 1 wen there's no entry in the table. And den it should increment the ID.

So I wrote below code for auto increment.
PHP Code:
include "Connections/database_connect.php";  
$q2 "select ID from products";
$result2=mysql_query($q2);
$num=mysql_numrows($result2);
$num++;
$ID $num
But still it gives problem. It starts with 0 and when I try to add entries it says:

Can't run query because Duplicate entry '0' for key 1.

Please help!

Thnks in advance.
Kan20 is offline   Reply With Quote
Old 05-12-2010, 12:27 PM   #2
Corrosive
Corrosive's Avatar
 
Join Date: Feb 2008
Location: Bristol, England
Posts: 7,105
Default

I don't think you can force the hand of auto_increment to my knowledge.
Corrosive is offline   Reply With Quote
Old 05-13-2010, 02:35 AM   #3
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 9,404
Default

mysql has the auto increment value . it will increment if you add an empty value '', or just create a new record
__________________
If you're happy and you know it shake your meds!
different style links examples


Bali Villas Flights to Bali
edbr is offline   Reply With Quote
Old 05-13-2010, 07:12 AM   #4
Kan20
 
Join Date: Jun 2009
Posts: 11
Default

Okay thanks. But is my above code correct?

~ K
Kan20 is offline   Reply With Quote
Old 05-13-2010, 07:54 AM   #5
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

Not at home at the moment to check, but I'm sure this can be done.

I will post later when I can get to my PC.

From memory tho', select the field tab of the Auto Increment Field. I think its in the dialogue that comes up.

Paul
tux is offline   Reply With Quote
Old 05-13-2010, 09:25 AM   #6
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

You can reset the auto increment to restart from 1 but you have to delete all records in the table. You can however set a higher number than the current one without deleting any records.

With the table open in SQL Manager choose Properties. Change the Auto Increment Value to zero to start again from 1 ( table must be empty ) or a higher number. Click refresh and save changes.

Last edited by tux; 05-13-2010 at 09:28 AM..
tux is offline   Reply With Quote
Old 05-13-2010, 07:28 PM   #7
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,258
Default

Please be careful here

The reason why it doesn't reset is to do with the mechanism of the auto increment and the relationship between tables.

Your tables could have foreign keys using the auto incrementing field as the primary key. When you delete a primary record you don't want to reassign that ID as there could be tables which could falsely form a relationship with another table.

You really don't need to worry about resetting this field. I write apps in a development environment. I move it to a UAT environment and then it goes live. By the time it goes live I have auto-incrementing values reaching well into the thousands. I don't reset them. There is no need
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk

Last edited by davidj; 05-13-2010 at 07:31 PM..
davidj is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:26 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com