Weird database problem

I have created a registration form with username and email set to unique in the User model. Now, at complete random I get errors that both the username and email are already registered.

I’ve tried registering with username set to “1” and email set to “1,” then both set to “2,” then “3” and so on. About every 3-5 fifth try I get an error that states the current email and username are already taken. They’re not and if I take a look at the user table after I receive the error, I can see that the username and email actually got registered with the latest ID despite the error.

From: class RegisterController extends Controller


    public function actionRegister()

    {   

        $model=new User('register');


        if(isset($_POST['user']))

        {

            $model->attributes=$_POST['user'];            

            $model->save();

        }


        $this->render('register',array(

            'model'=>$model,

        ));

    }

From: class user extends CActiveRecord


    public function rules()

    {

        return array(			

            array('username, email', 'unique', 'on'=>'register'),

	    array('id, username, password, email, reg_time, first_name, last_name, street_name, post_code, city, country', 'safe'),

	);

    }    


    public function beforeValidate(){

        if ($this->isNewRecord) 

            $this->reg_time = time();        

        return true;

    }




    public function beforeSave()

    {

        if ($this->isNewRecord)

            $this->password = md5($this->password);

        return true;

    }




Removing the unique rule doesn’t help. Then I just get a db error: “CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘10’ for key ‘username’”

Is this ajax submit ?, if you so I think your request is posted twice because of binding several times in some way.

Sadly no. What puzzles me is that it’s not every time. I can create 20 identical accounts, with username and email going from 1 to 20 and yet I’ll only get an error on about a fourth of the registrations.

could you post your table structure?

I finally got it solved. This was an Ajax problem after all.

Thanks for the help guys. :)