|
-
May 21st, 2002, 08:52 AM
#1
Thread Starter
Hyperactive Member
Error in MySql database
when I try to add new record to MySql datebase I face this error message:
---------------------------------------------
Error
SQL-query :
INSERT INTO `VisualBasicBank` (`BankID`, `Task`, `Author`, `EmailOrHomePage`, `DateSubmitted`, `Category`, `Url`, `Code`, `View`) VALUES ('130', 'NULLrty', 'NULLry', 'NULLrty', 'NULLrty', 'NULLrt', 'NULLrey', 'NULLery', 'NULLrey');
MySQL said:
Duplicate entry '127' for key 1
---------------------------------------------
How can I solve this problem. ?
note : when I remove row and try to add new one , it's added without problem but when I try to add new one it give me the same error.. What can I do ?
-
-
May 21st, 2002, 10:18 AM
#2
-
May 21st, 2002, 10:46 AM
#3
Thread Starter
Hyperactive Member
No error
there is not any error in code .. the code was work without any problem .. the error in database .. How can I repair the database..
also when I try to insert new row using phpmyadmin I face the same problem.
-
May 21st, 2002, 11:19 AM
#4
you are getting that error because there is already an Bankid # of hte one you try to insert. the only way I see to fix this is to delete that table and start fresh.
double check you id categorey for duplicates.make sure it is set to auto increment and it is primary
-
May 21st, 2002, 11:35 AM
#5
you are getting that error because there is already an Bankid # of hte one you try to insert. the only way I see to fix this is to delete that table and start fresh.
double check you id categorey for duplicates.make sure it is set to auto increment and it is primary
-
May 21st, 2002, 11:38 AM
#6
Thread Starter
Hyperactive Member
I do not want to delete table
the table is auto increment and it is primary.
the problem occur because there is row it's ID=127 and auto next ID=127 also, it must=128 .. I do not know why this error happen ..
to fix this problem I must let auto next ID=128 but how can I do that ?
also
If I can remove all spaces form datebase I think that will solve the problem .. Can I remove spaces from database ?
Thanks
-
May 21st, 2002, 11:41 AM
#7
yes that is why you are getting the error. the id category has to be auto increment, not the whole table. you can go in and edit the id field and change it so it is in order. or you can delete the table and start over with everything in order.
that is the problem with deleting rows so many times.
-
May 21st, 2002, 02:47 PM
#8
Stuck in the 80s
I always have weired autoincrement index errors too...
Is BankID the primary key? or is there another ID field?
What I always do, is count the number of rows. After that, and assuming that the ID's go in order without any numbers missing, I just set the ID with that number + 1. So:
PHP Code:
$numrows = mysql_num_rows(mysql_query("SELECT * FROM tablename")) + 1;
Then use the value stored in $numrows for the ID value. Just remember, as I already said, the ID's must not have any holes in it, ie, 1 2 3 5 6 8 10. Otherwise it wont work.
-
May 21st, 2002, 02:57 PM
#9
that is one way, but as I have seen it and this is what happens on mine. if you have rows like so
1 2 3 5 6 7 10
and you inserted a new record it should be 11, so this record will fill the first available hole (empty row) it finds and inserts the record. so it will look like this
1 2 3 11 5 6 7 10
that is if the ID field is set to auto-increment. mine works this way.
-
May 21st, 2002, 10:20 PM
#10
Stuck in the 80s
I already stated that it wont work if they aren't in order without holes.
-
May 21st, 2002, 11:37 PM
#11
I know and all I stated is that you don't need to do anything if ID field is auto icremented. Mysql will fill the holes with the new id.
-
May 22nd, 2002, 12:55 AM
#12
Thread Starter
Hyperactive Member
Yes
yes, BankID the primary key .....
-
May 22nd, 2002, 07:28 AM
#13
but is it set to auto increment? if yes then you will have to delete some or clean out the database as you have something internal going on.
-
May 22nd, 2002, 01:49 PM
#14
Stuck in the 80s
-
May 22nd, 2002, 02:06 PM
#15
huh
-
May 22nd, 2002, 02:06 PM
#16
Thread Starter
Hyperactive Member
The cause of error
I found out the cause of eror
the error happen because I set BankID as tinyint , the maximime of tinyint is 127 so error happend . I changed tinyint to int and the problem was solved.
Thanks
-
May 22nd, 2002, 02:42 PM
#17
PowerPoster
I use Medium Ints for auto incrementing fields; partly because when I set up a table in phpmyadmin with an auto incrementing field, it set the datatype to medium int (9) so I figured "why not?"
Whatsmore, I don't have any problems with auto increments. They're useful just to guarantee uniqueness
-
May 22nd, 2002, 03:11 PM
#18
Frenzied Member
Once you do an insert on a table with autoincrements, how do you know what the value was short of doing another select of the max?
-
May 22nd, 2002, 03:56 PM
#19
you can't and why would you want to. just do a select on it.
-
May 22nd, 2002, 04:54 PM
#20
PowerPoster
The trouble with doing a SELECT after an INSERT is that there is every possibly that another record may be inserted before the SELECT is carried out, esp. on a system with a lots of users.
You could either get into table locking, or use another method such as entering a unique string of numbers/chars into a field, then looking up the auto increment field from that.
-
May 22nd, 2002, 04:57 PM
#21
why would that matter? if you do a select of everything and then get the num_rows it will give you a general idea of what the id is at.
but like I said, why would you......
-
May 22nd, 2002, 05:01 PM
#22
PowerPoster
You might want to get the ID of the field you just inserted.
e.g., a customer's order is inserted into a database, you now want to tell them the ID of their order. The ID is an autoincrementing field..
Perfectly possibly scenario.
-
May 22nd, 2002, 05:06 PM
#23
sure don't play fair. 
ok if you are goin gthat way, then when the order is confirmed and you want to display the id, you would search for the username of that order, not the id, so you won't have to worry about another user ordering right behind you.
and if you don't keep the username in the order then you have problems from gate 1.
-
May 22nd, 2002, 05:12 PM
#24
PowerPoster
maybe you don't have a user-based system, people just enter a few details about themselves
What happens if they've made a few orders, you'd get more than 1 row returned 
Anyway, to answer the original question ("how do you know what the value was short of doing another select of the max"), you can't really becase of what I said, which is why you need to think of another way of doing it.
-
May 22nd, 2002, 06:12 PM
#25
Frenzied Member
So then it is not a good idea to use autoincrement for a primary key if you need to know the value.
-
May 22nd, 2002, 07:26 PM
#26
if you don't use auto increment then you are creating extra work for yourself. also it will lead to the same problem you asked about.
the reason I say that is you have to query the database and get the last id and then add 1 to it so you can insert the next ID. in the meantime somebody is doing the exact samething. so you aren't getting away from anything.
-
May 22nd, 2002, 07:41 PM
#27
PowerPoster
auto increment is a very useful feature, but the only way I can think of to get the ID of the record you just inserted is to insert some other unique identifier into the record which you know. Generating a random number is a good way.
That way, you can easily get the primary id of the correct record and not one that may have been inserted in the meantime
-
May 23rd, 2002, 08:09 AM
#28
Addicted Member
you dont always need to know the ID for the auto incrementing number before you insert it. All my inserts are on autoincrementing tables and if you just put a 0 in that field, it does it for ya.
INSERT into tblName VALUES (0, '$fname', '$lname');
and to grab that ID afterwards you can use the mysql_insert_id() function. I believe it takes the argument of the query so...
$theQuery = mysql_query ("INSERT into tblName VALUES(0, '$fname', '$lname')");
$theID = mysql_insert_id($theQuery);
or you use no argument and it grabs the last ID of the last query you've done.
-
May 23rd, 2002, 08:24 AM
#29
PowerPoster
wow, I can't believe I didn't know about mysql_insert_id()
Thanks man!
You can do it in the SQL statement too http://www.mysql.com/doc/G/e/Getting_unique_ID.html
-
May 23rd, 2002, 09:04 AM
#30
still doesn't solve what you said chris. somebody else could still add one to the database and you can still get the wrong id.
the best way is to have a unique category that you can query instead of the id.
and Golden: you don't have ot use a 0 just leave it blank
$theQuery = mysql_query ("INSERT into tblName VALUES('', '$fname', '$lname')");
-
May 23rd, 2002, 09:15 AM
#31
PowerPoster
Originally posted by scoutt
still doesn't solve what you said chris. somebody else could still add one to the database and you can still get the wrong id.
I assumed they would have covered that possibilty...oh well
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|