Re: Re: php can't insert data mysql table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hey thanks guys adding debugging info worked.

Actually it was mysqli_error() providing me with a specific error of where
the problem was.

Cannot insert query:Duplicate entry '0' for key 'PRIMARY'

This is the data in the table

mysql> select * from guitarwars;
+----+---------------------+-------------------+-------+-------------------+
| id | date                | name              | score | screenshot        |
+----+---------------------+-------------------+-------+-------------------+
|  0 | 2012-10-01 11:55:45 | Tommy Tutone      |  2442 | bg_titlestrip.jpg |
|  1 | 2012-10-01 08:34:18 | Dunphy            | 20000 | proof.jpg         |
|  2 | 2012-10-01 00:25:53 | ray davies        |  NULL | 2241              |
|  3 | 2008-04-22 14:37:34 | Paco Jastorius    |  NULL | NULL              |
|  4 | 2008-04-22 21:27:54 | Nevil Johansson   |  NULL | NULL              |
|  5 | 2008-04-23 09:12:53 | Belita Chevy      |  NULL | NULL              |
|  6 | 2008-04-23 14:09:50 | Kenny Lavitz      |  NULL | NULL              |
|  7 | 2008-04-24 08:13:52 | Phiz Lairston     |  NULL | NULL              |
|  8 | 2008-04-25 07:22:19 | Jean Paul Jones   |  NULL | NULL              |
|  9 | 2008-04-25 11:49:23 | Jacob Scorcherson |  NULL | NULL              |
+----+---------------------+-------------------+-------+-------------------+


This was the query I was using:

$query = "INSERT INTO guitarwars  (date, name, score, screenshot) VALUES
(NOW(), '$name', '$score', '$screenshot')";

It seems to be inserting a default value of 0 since the id is not being
specified and that's when I realized that I had failed to auto_increment
the id column. D'oh! So once I did that everything worked like a charm.

@Ken
>First -- NEVER post code with your database username/password. Since you
did, change your db password immediately.

Well actually I did not. Did you really think 'secretsauce' was my
password? :) But I guess you're right in that this may be a little
ambiguous when seeking advice in lists so from now on I will take your
advice on making login information unambiguously fake in the form of
user='xxxx' and password='xxx'.

@Stuart
But take note of what everyone else is saying. You should be getting the
error message when this happens which will tell you exactly what the
problem is, above and beyond "Cannot insert query" (which, btw, makes no
sense at all :)).

Ok well I'm using an 'insert' query so I'm not sure why you say that this
makes no sense at all. :)) If you don't mind giving this n00b advice what
would be a better/more accurate error message?

They take away from this for me was.. don't skimp on the error messages!
The one I got was so clear that fixing the problem was easy at that point.

But thanks again guys.. this list has been an indispensable source source
of wisdom on my journey in learning PHP.

Tim


Thanks again guys,
Tim



-- 
GPG me!!

gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux