943,877 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7840
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 17th, 2008
0

Duplicate entry '' for key 2

Expand Post »
hi everyone,
i keep on getting this error when i try to insert a value into a table via php.
Duplicate entry '' for key 2

i've tried a lot of solutions from the web including changing the the primary key from int to bigint. i also flushed the database and deleted all entries from the database but i still got that error. i could insert records from the nysql command prompt but not from the php script.
does anyone know what might be wrong?
the table in question looks like this:

sql Syntax (Toggle Plain Text)
  1. CREATE TABLE `article_fields` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `field_title` VARCHAR(75) NOT NULL,
  4. `field_type` VARCHAR(75) NOT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=INNODB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


and my php code to insert data into the table looks like this:
PHP Syntax (Toggle Plain Text)
  1. $sql = 'INSERT INTO article_fields VALUES("NULL","'.$new_text.'","'.$field_type.'")';
  2. $insert = mysql_query($sql) or die(mysql_error());
Last edited by peter_budo; Nov 20th, 2008 at 12:42 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

you can't have the same primary key twice. hence the "duplicate entry". you are setting it as NULL. just omit the primary key entry in the values all together.

like this:

PHP Syntax (Toggle Plain Text)
  1. $sql = "INSERT INTO `article_fields` (`field_title`,`field_type`) VALUES ('{$new_text}','{$field_type}')";
Last edited by kkeith29; Nov 18th, 2008 at 12:10 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

Because you have set the primary key to be auto incremented so you don't need to pass anything here.
Reputation Points: 13
Solved Threads: 1
Newbie Poster
jack239 is offline Offline
14 posts
since Aug 2008
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

Keith,

You can insert records in a table exactly as cali_dotcom does, it is not oblgatory to do it by mentioning the column names where you want to insert data. Your approach is OK when you want to insert data in different order than the columns in the table.
The problem with the script is that "NULL" is different from NULL without quotes. MySQL considers "NULL" to be a string and does not validate it as the value NULL.

So this should work perfectly
PHP Syntax (Toggle Plain Text)
  1. $sql = 'INSERT INTO article_fields VALUES(NULL,"'.$new_text.'","'.$field_type.'")';

Also, when inserting numbers it is good to insert them not surrounded by "" as MySQL validates numbers correctly.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

i've tried every single suggestion i found on the web, but i still wont work. actually, it worked just one time and then i clicked again, and then it gave me the same error again. i,ve tried flushing the table, emptying the table checking and repairing the table from phpmyadmin.

does anyone know what could still be wrong?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

I suggest you do the following :

echo $sql;

Then simply copy the string from your browser and paste it into phpmyadmin sql statement and run it.
If it shouts an error, and it is still the same, paste here the sql string and we'll see. I don't see anything wrong there, but you never know, a single ' may ruin the whole thing...
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

i ran this code on php myadmin:
INSERT INTO `article_fields`(`field_title`, `field_type`) VALUES("'.$new_text.'","'.$field_type.'")
and it worked. the problem is running that code from a php script. did i mention that i was using the zend framework.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

No, you did not mention the ZEND.
However, this insert as you have typed it contains the variable names instead of variable values. Is this the php code or the result from the php echo command?

I am not 100% sure you need the table and field names surrounded by ' ', but, still if it works, maybe it is ok.
Why don't you try this code. It works fine for me in multiple pages, so I have it verified numerous times.

PHP Syntax (Toggle Plain Text)
  1. $sql = 'INSERT INTO article_fields VALUES (NULL, \''.$new_text.'\', \''.$field_type.'\')';

I think that this code will be ok also for the zend framework.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

Click to Expand / Collapse  Quote originally posted by Rhyan ...
However, this insert as you have typed it contains the variable names instead of variable values. Is this the php code or the result from the php echo command?

.
i'm not sure what you mean but those are vaiables i got from a command like:

$new_text = $_GET['new_text'];
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008
Nov 18th, 2008
0

Re: Duplicate entry '' for key 2

Click to Expand / Collapse  Quote originally posted by Rhyan ...

PHP Syntax (Toggle Plain Text)
  1. $sql = 'INSERT INTO article_fields VALUES (NULL, \''.$new_text.'\', \''.$field_type.'\')';
i just tried this code but it still didn't work.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Urgent help needed! Transaction process after shopping cart
Next Thread in PHP Forum Timeline: how to access accessing PHP variable in JavaScript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC