# How do I get this code to work!

**URL:** <https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862>\
**Category:** programming\
**Created:** [June 6, 2006, 7:45pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862 "2006-06-06T19:45:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![skyler](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@skyler](https://forum.kirupa.com/u/skyler)\
**Post date:** [June 6, 2006, 7:45pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/1 "2006-06-06T19:45:32Z")

</div>

I cant seem to get this right. I have created if statements to diagnose where I keep on having the query error, and just like i thought, it was in the INSERT INTO code. Here is the code, can someone look at this for me and see if The code is in anyway wrong, (misspelling, punctuation and etc.?)

```php
//error here.
   $query = "INSERT INTO register (id, company, att, racernum, phone, names, date) VALUES ('', '$company', '$att', '$racernum', '$phone', '$names', '$date')";

   mysql_query($query) or die('Error, query failed');

exit();

```

---

<div class="post-metadata">

**Author:** ![DHDesign](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@DHDesign](https://forum.kirupa.com/u/DHDesign)\
**Post date:** [June 6, 2006, 7:54pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/2 "2006-06-06T19:54:48Z")

</div>

why are you putting a blank value into the id field?  
if its an auto-increment primary key, then just leave that alone and it will automatically add it itself.

---

<div class="post-metadata">

**Author:** ![DHDesign](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@DHDesign](https://forum.kirupa.com/u/DHDesign)\
**Post date:** [June 6, 2006, 7:56pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/3 "2006-06-06T19:56:42Z")

</div>

also, try two things:

1. print out all your variables that you are inserting before the query…this way u can make sure that ur variables have no problems

2. print out the query (ie. print $query;)  
this will show you wat you are trying to do so u can identify the problem in the query

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [June 6, 2006, 7:58pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/4 "2006-06-06T19:58:16Z")

</div>

try:

```php
$query = "INSERT INTO register (company, att, racernum, phone, names, date) VALUES ('$company', '$att', '$racernum', '$phone', '$names', '$date')";
mysql_query($query) or die(mysql_error());

```

adding to what DHDesign said, if your id field is auto\_increment, you should not be inserting any id value. it will automatically populate.

make sure your die’s are mysql\_error() when testing so you get a better error message.

---

<div class="post-metadata">

**Author:** ![skyler](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@skyler](https://forum.kirupa.com/u/skyler)\
**Post date:** [June 6, 2006, 8:12pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/5 "2006-06-06T20:12:10Z")

</div>

ok. Well, I seem to still be getting an error, but the detailed error did give me a different suggestion as to what the problem is, but I have no idea what the error means. I get this as the error.

“Duplicate entry ‘0’ for key 1”

---

<div class="post-metadata">

**Author:** ![skyler](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@skyler](https://forum.kirupa.com/u/skyler)\
**Post date:** [June 6, 2006, 8:14pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/6 "2006-06-06T20:14:03Z")

</div>

Okay, I gotta be a retard, I just forgot to put the row as “auto increment”

hello.

well, thanks to everyone who helped, there were some tips that will be very useful.

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [June 6, 2006, 8:28pm UTC](https://forum.kirupa.com/t/how-do-i-get-this-code-to-work/189862/7 "2006-06-06T20:28:25Z")

</div>

no problem. for those that didn’t understand, the  
"Duplicate entry ‘0’ for key 1" error message was because the field was set as a primary key, but skyler was unintentionally trying to insert a duplicate value. to fix the problem, just set as auto increment and make sure the starting auto increment number does not already exist in the table.
