Jump to content

Duplicate entry '1' for key 'PRIMARY'


Birdzview

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());
?>
 
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 ?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.