Duplicate entry '' for key 2
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:
CREATE TABLE `article_fields` (
`id` int(11) NOT NULL auto_increment,
`field_title` varchar(75) NOT NULL,
`field_type` varchar(75) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
and my php code to insert data into the table looks like this:
$sql = 'INSERT INTO article_fields VALUES("NULL","'.$new_text.'","'.$field_type.'")';
$insert = mysql_query($sql) or die(mysql_error());
cali_dotcom
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
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:
$sql = "INSERT INTO `article_fields` (`field_title`,`field_type`) VALUES ('{$new_text}','{$field_type}')";
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
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?
cali_dotcom
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
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.
cali_dotcom
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
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'];
cali_dotcom
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
$sql = 'INSERT INTO article_fields VALUES (NULL, \''.$new_text.'\', \''.$field_type.'\')';
i just tried this code but it still didn't work.
cali_dotcom
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
the ` (which are different than ') are not necessary, but I see phpMyAdmin and other programs use them a lot so it kind of became a habit of mine. it also helps me identify database and table names in the queries i write. i wrote the column names in so we could avoid silly errors that i figured s/he would have.
cali_dotcom:
if you have some code to look at, post it. maybe we can find if something else is preventing this from working, because i know the sql i posted works.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
mysql_query(sprintf("insert into article_fields
(new_text, fields_type) values
('%s', '%s');",
$new_text, $field_type))
almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376