Page 1 of 1

Following the steps...

Posted: Tue Apr 10, 2012 3:41 pm
by Acidzero
Hello there!!! i am a noob in coding php so and i begin with the video tutorial of making a browser game by the way is awesome, anyway i get mysql error when i try to create my first 2 creatures in my "creatures table"...

Error

INSERT INTO `tutorial`.`creatures` (
`id` ,
`name` ,
`hppoints` ,
`attack` ,
`defense`
)
VALUES (
'', 'goblin', '5', '3', '3'
), (
'', 'orc', '10', '6', '6'
)

#1062 - Duplicate entry '0' for key 'PRIMARY'

I follow step by step the tutorial and i get this error again and again and i dont know how to fix it, any ideas? Thx :roll:

Re: Following the steps...

Posted: Tue Apr 10, 2012 4:18 pm
by Jackolantern
There are a couple of things here. First, since you are getting a duplicate error, that means you have not set your 'id' field in the table to "auto-increment". Setting that option means that each insertion into the table will increment the id entered by +1. This means you can leave it out and just let the database handle it itself. Therefore, once you make sure your table has the id field auto-incremented, remove the 'id' part out of your query.

After doing that, the next error this query will cause (or should cause, at least, provided you set your column data types correctly) is a "type mismatch". This is because you have put quotes around the actual numbers being inserted (for example, for the hppoints column, you are adding '5'). This makes MySQL attempt to add that data to the table as text instead of numbers. You only want to put quotes around values that actually are text, such as the name of the creature. If numbers get taken as text, then MySQL is going to error out when it tries to add that to the table and sees the hppoints, attack and defense columns are set up to be numerical data and not strings (text). So be sure to remove the quotes around your numbers!

Hope this helps!

Re: Following the steps...

Posted: Wed Apr 11, 2012 12:41 pm
by Acidzero
You were right i forgot to set "auto-increment" for the id, problem solved. I still have a long way to learn how mysql works. Thx a lot for the help!!!

Re: Following the steps...

Posted: Wed Apr 11, 2012 3:02 pm
by Foofighter
Hi,
Another way is to build you tables in phpmyadmin without the mysql code, its a bit easier at the beginning. ;)

greetz

Re: Following the steps...

Posted: Wed Apr 11, 2012 3:50 pm
by Acidzero
Foofighter wrote:Hi,
Another way is to build you tables in phpmyadmin without the mysql code, its a bit easier at the beginning. ;)

greetz
Hello there, i am already using phpmyadmin :)