Birdzview Posted March 25, 2013 Share Posted March 25, 2013 Please help, anybodyDuplicate entry '1' for key 'PRIMARY' - The message I get when running my moviedata.php See also moviecreate.php that creates the databaseHere 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 More sharing options...
Barand Posted March 25, 2013 Share Posted March 25, 2013 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 Link to comment https://forums.phpfreaks.com/topic/276122-duplicate-entry-1-for-key-primary/#findComment-1420910 Share on other sites More sharing options...
Birdzview Posted March 25, 2013 Author Share Posted March 25, 2013 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 ? Link to comment https://forums.phpfreaks.com/topic/276122-duplicate-entry-1-for-key-primary/#findComment-1420938 Share on other sites More sharing options...
mikosiko Posted March 25, 2013 Share Posted March 25, 2013 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. Link to comment https://forums.phpfreaks.com/topic/276122-duplicate-entry-1-for-key-primary/#findComment-1420940 Share on other sites More sharing options...
Birdzview Posted March 25, 2013 Author Share Posted March 25, 2013 Hi Mikosiko, Thank you ! I removed the PRIMARY reference in the INSERT and also the digits in VALUES and it worked Appreciated Link to comment https://forums.phpfreaks.com/topic/276122-duplicate-entry-1-for-key-primary/#findComment-1420950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.