Hello all, i get this error while inserting and udating some tables.Duplicate entry '9105224' for key 'PRIMARY'error no:1062error:1062 .number 9105224 is randomly generated and set in u_id field in one of the tables in MYSQL, which is set to unique and primary.The sql querries are working fine and the rows are also affected.but it gives me duplicate entry error..plz help..Is it bcoz at first nunber is generated and checked against existing numbers in u_id field and if match is found it gives error and immediately generates another random number ( but in this case how can random number be generated again??i havnt written any code for such duplicate cases where call can be made to rand() ) and writes in place of u_id again with new entry??..
HELP ME OUT..SOS!! :|

if i understand you insert a random number into your primary key ?
if so, i dont think you can do that, cause even if theyre random, at some point youll end up with the same entry.

if im in the ditch, please give us more details and maybe show the code where the error accurs.

This can happen also if you send an empty string twice. Post your code.

ok guys..hav a look at this.
i googled for this..i found something releated to size of int(8) for u_id..i hcanged it to int(10) but didnt worked.

$q=rand(0,99999999);
$sql="UPDATE tab1 SET col1=$p WHERE col2=$p AND col1 IS NULL";
mysql_query($sql);
$sql_two="INSERT INTO user(u_id,u_uname, u_fname, u_lname)VALUES ($q,'$username','$fname','$lname')";
mysql_query($sql_two);
if ((!(mysql_query($sql,$con))) || (!(mysql_query($sql_two,$con))))
    {
        if(mysql_errno()==1062)
                {
               $error_flag=0; 

               echo"in if < br/>";
                echo mysql_error();
               echo "error no:".mysql_errno();
                die("error:".mysql_errno());
                }
        $error_flag=0;
    }
else
    {
   $error_flag=1;

    }

yes, like i said earlier nothing is preventing the rand() function to generate the same number twice, no matter how big is your int. Of course bigger the int and less likely your gonna get the same number twice, but still... you cant insert the same primary key twice.

maybe you just want to add a small filter on your random numbers, just to make sure they dont try generate the same number twice.

Thing is even though i get error about duplicate entry for field u_id..the record and the random number generated is inserted in the table at the field u_id which is primary and unique.And my database is also not so huge.Hardly i have 30 entries.

Besides why don't you use auto_increment? With that you don't even need to use unique key:

u_id int(10) not null auto_increment primary key

auto_increment is fine but not good. i think user can guess the successive numbers if the numbers are incremented in sequence or with some interval.i think i'll go with hash functions..may be i'll solve it with it..Thanks all for ur reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.