Jump to content

Duplicate entry '1' for key 'PRIMARY'


Go to solution Solved by mikosiko,

Recommended Posts

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
code tags
Link to comment
https://forums.phpfreaks.com/topic/276122-duplicate-entry-1-for-key-primary/
Share on other sites

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 ?

  • Solution

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.