Jump to content


Photo

Duplicate entry '1' for key 'PRIMARY'


Best Answer mikosiko, 25 March 2013 - 11:27 AM

if you declared in your table that movie_id is an auto-increment column then your INSERT (below) is wrong

 

 

$insert = "INSERT INTO movie(movie_id, movie_name, movie_type, " .
"movie_year, movie_leadactor, movie_director) " .
"VALUES(1, 'Bruce Almighty', 5, 2003, 1, 2), " .
"(2, 'Office Space', 5, 1999, 5, 6), " .
"(3, 'Grand Canyon', 2, 1991, 4, 3)";
 

it shouldn't contain references to the movie_id column or values for it.

 

and if you are getting the duplicate row error, means that you are executing the same code more than once.

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 Birdzview

Birdzview

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 March 2013 - 06:01 AM

Please help, anybody

Duplicate entry '1' for key 'PRIMARY' - The message I get when running my moviedata.php See also moviecreate.php that creates the database
Here are the parts involved :

A) MOVIEDATA.PHP (THE PROBLEM ONE)

 

<?php
//insert data into "movie" table
$insert = "INSERT INTO movie(movie_id, movie_name, movie_type, " .
"movie_year, movie_leadactor, movie_director) " .
"VALUES(1, 'Bruce Almighty', 5, 2003, 1, 2), " .
"(2, 'Office Space', 5, 1999, 5, 6), " .
"(3, 'Grand Canyon', 2, 1991, 4, 3)";
$results = mysql_query($insert)
or die(mysql_error());
?>
 

 



B) CREATEMOVIE.PHP (WORKS JUST FINE)

 

<?php

//connect to MySQL
$connect = mysql_connect("localhost", "root", "") or
die ("Hey boy, check your server connection.");

//create the main database if it doesn't already exist
$create = mysql_query("CREATE DATABASE IF NOT EXISTS moviesite")
or die(mysql_error());

//make sure our recently created database is the active one
mysql_select_db("moviesite");

//create "movie" table
$movie = "CREATE TABLE movie (
movie_id int(11) NOT NULL auto_increment,
movie_name varchar(255) NOT NULL,
movie_type tinyint(2) NOT NULL default 0,
movie_year int(4) NOT NULL default 0,
movie_leadactor int(11) NOT NULL default 0,
movie_director int(11) NOT NULL default 0,
PRIMARY KEY (movie_id),
KEY movie_type (movie_type, movie_year)
)";

$results = mysql_query($movie)
or die (mysql_error());
?>
 

Edited by fenway, 29 March 2013 - 11:04 PM.
code tags


#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,134 posts
  • LocationCheshire, UK

Posted 25 March 2013 - 06:46 AM

One of the more self-explanatory error messages.

 

You are obviously trying to add a record with id=1 when there is already a record in the table with that id


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 Birdzview

Birdzview

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 March 2013 - 11:10 AM

Yes, that's exactly the problem - but how shall I fix it ?
Please look at the code of CREATEMOVIE.PHP that generated the table - does the problem lie here ?
Or does the problem lie with the data itself that I want to upload from MOVIEDATA.PHP ?
Where do I find the table that was generated to see the data in it ?

#4 mikosiko

mikosiko

    this mat is so comfy!!

  • Members
  • PipPipPip
  • 1,263 posts

Posted 25 March 2013 - 11:27 AM   Best Answer

if you declared in your table that movie_id is an auto-increment column then your INSERT (below) is wrong

 

 

$insert = "INSERT INTO movie(movie_id, movie_name, movie_type, " .
"movie_year, movie_leadactor, movie_director) " .
"VALUES(1, 'Bruce Almighty', 5, 2003, 1, 2), " .
"(2, 'Office Space', 5, 1999, 5, 6), " .
"(3, 'Grand Canyon', 2, 1991, 4, 3)";
 

it shouldn't contain references to the movie_id column or values for it.

 

and if you are getting the duplicate row error, means that you are executing the same code more than once.


Edited by mikosiko, 25 March 2013 - 11:29 AM.

 
ini_set("display_errors", 1);
error_reporting(-1);

#5 Birdzview

Birdzview

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 March 2013 - 12:58 PM

Hi Mikosiko,
Thank you ! I removed the PRIMARY reference in the INSERT and also the digits in VALUES and it worked
Appreciated




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com