سؤال

I made a column called password and when users try creating an account with similar passwords, I want to receive "Duplicate entry for key" error.

Duplicate entry 'admin' for key 'password_2' I do not have a password_2 column, just a column called "password".

I have tried making the password column unique, still doing the same thing. Any ideas?

Here is the table schema

CREATE TABLE admin (  
    id int(16) NOT NULL AUTO_INCREMENT,  
    email varchar(32) NOT NULL, 
    firstName varchar(32) NOT NULL,  
    lastName varchar(32) NOT NULL,  
    password varchar(32) NOT NULL,  
    DateOfBirth date NOT NULL,  
    Gender char(1) NOT NULL,  
    phoneNumber varchar(50) NOT NULL, 
    PRIMARY KEY (id), 
    UNIQUE KEY password_2 (password), 
    UNIQUE KEY email (email), 
    UNIQUE KEY password_3 (password), 
    UNIQUE KEY password_4 (password), 
    UNIQUE KEY password_5 (password), 
    KEY phoneNumber (phoneNumber) 
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1
هل كانت مفيدة؟

المحلول

You need to remove the unique index from the password and rather add it to users

I have tried making the password column unique, still doing the same thing. Any ideas?

this is the reason why you are getting this error message Duplicate entry for key

From your schema Remove this lines

UNIQUE KEY password_2 (password), 
UNIQUE KEY password_3 (password), 
UNIQUE KEY password_4 (password), 
UNIQUE KEY password_5 (password), 

your new schema will look like

CREATE TABLE admin (  
    id int(16) NOT NULL AUTO_INCREMENT,  
    email varchar(32) NOT NULL, 
    firstName varchar(32) NOT NULL,  
    lastName varchar(32) NOT NULL,  
    password varchar(32) NOT NULL,  
    DateOfBirth date NOT NULL,  
    Gender char(1) NOT NULL,  
    phoneNumber varchar(50) NOT NULL, 
    PRIMARY KEY (id), 
    UNIQUE KEY email (email), 
    KEY phoneNumber (phoneNumber) 
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top