# PHP code for auto increment



## Kan20 (Jul 20, 2009)

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.

```
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.


----------



## Redcore (Aug 14, 2007)

Have you tried TRUNCATE instead of DELETE to flush your table? That should solve your increment problem.


----------

