Following the steps...

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Acidzero
Posts: 3
Joined: Tue Apr 10, 2012 3:00 pm

Following the steps...

Post 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:
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Following the steps...

Post 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!
The indelible lord of tl;dr
User avatar
Acidzero
Posts: 3
Joined: Tue Apr 10, 2012 3:00 pm

Re: Following the steps...

Post 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!!!
User avatar
Foofighter
Posts: 121
Joined: Sun Mar 04, 2012 1:52 pm

Re: Following the steps...

Post by Foofighter »

Hi,
Another way is to build you tables in phpmyadmin without the mysql code, its a bit easier at the beginning. ;)

greetz
User avatar
Acidzero
Posts: 3
Joined: Tue Apr 10, 2012 3:00 pm

Re: Following the steps...

Post 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 :)
Post Reply

Return to “Beginner Help and Support”