Duplicate entry 'something' for key 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepaks85
    New Member
    • Aug 2006
    • 114

    Duplicate entry 'something' for key 2

    Dear Sir,

    I have setup a SQL database with PHP. Whenever I am trying to insert the data into my database it gives me error : 'Duplicate entry for key 2'. Please help me sir.

    I am sending you the coding for insert data.


    <?
    include("sessio n.php");
    session_start() ;
    include("config .php");

    $iata = $_POST['iata'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $companyname = $_POST['companyname'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $province = $_POST['province'];
    $country = $_POST['country'];
    $phone = $_POST['phone'];
    $fax = $_POST['fax'];
    $cemail = $_POST['cemail'];


    $query = ("INSERT INTO $SQL_USR_TBL (iata, name, email, username, password, companyname, date, city, state, province, country, phone, fax, cemail) values('$iata', '$name', '$email', '$username', '$password', '$companyname', now(), '$city', '$state','$prov ince', '$country','$ph one','$fax','$c email')");

    $result = mysql_query($qu ery);
    if(!@mysql_quer y ($query)) {
    echo mysql_error();
    }else{

    echo "Thank you for registration.";
    //header("locatio n:main.php");
    exit;
    }

    mysql_close();
    ?>





    And below are the database setup fields:


    CREATE TABLE `user` (
    `id` int(11) NOT NULL auto_increment,
    `iata` int(14) NOT NULL default '0',
    `name` varchar(255) NOT NULL default '',
    `email` varchar(255) NOT NULL default '',
    `username` varchar(255) NOT NULL default '',
    `password` varchar(255) NOT NULL default '',
    `companyname` varchar(255) NOT NULL default '',
    `date` datetime NOT NULL default '0000-00-00 00:00:00',
    `city` varchar(255) NOT NULL default '',
    `state` varchar(255) NOT NULL default '',
    `province` varchar(255) NOT NULL default '',
    `country` varchar(255) NOT NULL default '',
    `phone` int(255) NOT NULL default '0',
    `fax` int(255) NOT NULL default '0',
    `cemail` varchar(255) NOT NULL default '',
    PRIMARY KEY (`id`),
    UNIQUE KEY `username` (`username`)
    ) TYPE=MyISAM AUTO_INCREMENT= 8 ;




    Please help me Sir.

    Thanks
    Deepak
  • michaelb
    Recognized Expert Contributor
    • Nov 2006
    • 534

    #2
    There are two unique keys in your table, one is on column id, which gets its value automatically from a sequence generator, another one is on the column "username"

    I think that the error you're getting is generated when you're trying to insert a duplicate value into this column.
    Considering what's in the table you probably don't want to have multiple records for the same username, so I guess you have two options here:
    1. check whether given username exists before doing insert.
    If it exists issue the appropriate warning and ask user to select a different username.
    2. if this is an existing user than perhaps the purpose would be to update his or her record with new data, instead of creating a new record.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      deepak: after having started 13 threads you should know by now that you have to show your code with php, code or html tags!!

      It is time you read the Posting Guidelines that apply to this forum.

      moderator :cool:

      Comment

      • michaelb
        Recognized Expert Contributor
        • Nov 2006
        • 534

        #4
        Ah, I've been quilty too, thanks for reminder!
        michael

        Comment

        Working...