Jump to content


Photo

auto_increment


  • Please log in to reply
3 replies to this topic

#1 aysx

aysx
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 March 2003 - 06:06 AM

create table testtable (
id int(4) not null auto_increment,
name varchar(30),
primary key (id) )

say i add some names to testtable. each name has an id, and the id is auto_increment. i have 50 names. then i decide to delete all the names except the first name i added to the database (that has an id of 1).

THEN i add another name to the database. i expect the id of the name to be 2, but instead it\'s... 51. o_O; kind of logical, seeing it\'s the fifty-first name i added to the database, no matter if i deleted the rest.

.. but i don\'t WANT it to be 51! i want it to be 2! 1-> 2, not -> 51! -___-; does anyone know if you can fix this? or is this just my computer acting up again X_x;

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 31 March 2003 - 02:03 AM

Yup! thats the problem with auto increment.....

Now the best way would be to remove the auto increment and use a simple quesry to detect the count and add that

i.e do a count and say u get 1 just do 1 + 1 =2 which would be ur next id number.
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 02 April 2003 - 12:04 AM

I can\'t get it to work...

what do I have to put as the ID for whatever I insert into my table if I use auto_increment?

I have the users define the first, last, age, and race variables on the previous page, and this is my code for the MySQL page:

[php:1:f7325e52b1]<?php

$conn=mysql_connect (\"localhost\", \"burnttoa_tehuser\", \"tehpass\") or die (\'I cannot connect to the database because: \' . mysql_error());
mysql_select_db (\"burnttoa_tehdb\",$conn);

$q1 = mysql_query(\"CREATE TABLE roleplayers (id int(4) not null auto_increment, first varchar(255), last varchar(255), age varchar(255), race varchar(255), PRIMARY KEY (id), UNIQUE id (id))\",$conn);
if ($q1==FALSE) {
print \"<code><b>Error:</b> Creation of table \"roleplayers\" failed!<br></code>\";
} else {
print \"<code>Creation of table \"roleplayers\" was sucessful!<br></code>\";
}

$q2 = mysql_query(\"INSERT INTO roleplayers VALUES (auto_increment,\'$first\',\'$last\',\'$age\',\'$race\')\",$conn);
if ($q2==FALSE) {
print \"<code><b>Error:</b> Something went wrong while adding your information!<br></code>\";
} else {
print \"<code>Succesfully added your information!<br></code>\";
}

mysql_close($conn);

?>[/php:1:f7325e52b1]

The first query (creating the table) goes fine, but then when I try to add the data it gives me the error message.

If I just set the id as 1, it will work correctly, but I need it to set the id to 2 if they add another thing and so on.

EDIT: Ahh... I get it now. I don\'t set the data ID to literally auto_increment, he just meant he was using auto_increment to increase the data IDs. But, still, how do I get it to work?
DEFINE("YOU","Spending Too Much Time On The Computer");

#4 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 02 April 2003 - 02:26 AM

Nevermind, figured it out, just leave the id field blank.
DEFINE("YOU","Spending Too Much Time On The Computer");




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users