Can someone help me with this mysql error?

Discussion in 'MySQL' started by GoWFB_SEO, Jun 3, 2006.

  1. #1
    I was reading a example of inserting data to mysql in php script, I tried the following, but it gave me an error message :"Duplicate entry '1' for key 1 "

    $connect= mysql_connect("localhost", "testuser", "testpass")or die ("Are you trying to hack me?");
    //Create database
    $create=mysql_query("CREATE DATABASE IF NOT EXISTS moviesite") or die(mysql_error());
    mysql_select_db("moviesite");

    //insert data into "movie" table
    $insert="INSERT INTO movie (movie_id, movie_name, movie_type, "." movie_year, movie_leadactor, movie_director)"
    ." VALUES (1, 'Bruce Alm', 5, 2003, 1, 2),"
    ." (2, 'office space', 2, 1991, 4, 3)";

    $results=mysql_query($insert) or die(mysql_error());

    //insert into "movietye" table
    $type="INSERT INTO movietype (movietype_id, movietype_label)"
    ." VALUES (1, 'Sci Fi'),"
    ." (2, 'Drama'),"
    ." (3, 'Adve'),"
    ." (4, 'War'), "
    ." (5, 'Comedy'),"
    ." (6, 'Horror'),"
    ." (7, 'Action'),"
    ." (8, 'Kids')";
    $results=mysql_query($type) or die(mysql_error());

    //insert data to people table
    $people ="INSERT INTO people (people_id, people_fullname, ". people_isactor, "people_isdirector)" .
    " VALUES (1, 'Jim Carrey', 1, 0)," .
    " (2, 'Tom Shadyac', 0, 1), " .
    " (3, 'Lawrence Kasdan', 0, 1), " .
    " (4, 'Kevin Kline', 1, 0), " .
    " (5, 'Ron Livingston', 1, 0), " .
    " (6, 'Mike Judge', 0, 1)";
    $results=mysql_query($people) or die(mysql_error());

    echo "Data inserted successfully!";


    Can someone clarify it for me? and also, i am not sure why there is a '.' at the end of the line
     
    GoWFB_SEO, Jun 3, 2006 IP
  2. woodside

    woodside Peon

    Messages:
    182
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    "Duplicate entry '1' for key 1" means you have a unique index (key) and you are trying to insert a value into that field that is already there.
     
    woodside, Jun 3, 2006 IP
  3. sadcox66

    sadcox66 Spirit Walker

    Messages:
    496
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your index key is marked Unique
    So you can only have one piece of each data.

    Example. You can insert three rows
    one
    two
    three

    but you cannot insert
    one
    one
    one
     
    sadcox66, Jun 3, 2006 IP
  4. websys

    websys Active Member

    Messages:
    841
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    78
    #4
    if you are inserting data into a existing table , try to select all and empty table ( not drop table ) before you start inserting new data .
     
    websys, Jun 4, 2006 IP