MYSQLi/PHP allowing duplicate entry on UNIQUE? - mysql

I am having a very strange problem.
This is what i have, in my structure the "email" field is clearly set to unique. However when i try to register with a duplicate email, instead of giving me a fat error it lets it slide. My database is having two rows with the same email..
The problem isnt just for email, even though i have them all set to unique it allows any and every field to have duplicates..
Any suggestions as to why this may happen? The field is not set to allow null.

MYSQLi/PHP allowing duplicate entry on UNIQUE?
Nope.
It is some mistake with your code or data.

Your current table structure is
I can't see any unique key defined in the table that's why it is possible to have duplicate email, try this:
CREATE TABLE UserList
(
ID INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
username VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
igname VARCHAR(50) NOT NULL,
password VARCHAR(100) NOT NULL,
verified VARCHAR(5) NOT NULL,
CONSTRAINT email_uq UNIQUE (email)
);

Related

Foreign Key value in child table on MariaDB shows duplicated value from parent table

I was just started creating some database. Very basic, two tables only, and I add this one foreign key, JUST LIKE most tutorials.
Here's what happened, step by step...
I created table UserRole:
IDUserRole int not null auto_increment,
UserRole varchar(8) not null,
primary key (IDUserRole),
index (UserRole)
then I added some data:
IDUserRole = 1, UserRole = "ADMIN"
IDUserRole = 2, UserRole = "UKM"
then I created another table called UserName:
IDUserName int not null auto_increment,
UserName varchar(50) not null,
UserRole varchar(8) not null,
primary key (IDUserName),
index (UserRole),
constraint fkfk foreign key (UserRole) references userrole (UserRole)
on delete restrict on update cascade
then I tried to input a value in the UserRole field, on UserName table. Something's funny came up when I click on the value:
It shows not only one, but TWO values separated with '-', TWICE... Like this for the simpler picture:
ADMIN-ADMIN
UKM-UKM
ADMIN-ADMIN
UKM-UKM
In some cases, it's gone like this:
ADMIN-Lam Johannes
UKM-blablabla
SENDER-blablabla
SELLER-blbablba
...
Lam Johannes-ADMIN
blablabla-UKM
blablabla-SENDER
blbablba-SELLER
So anyone please, why did this occur? And how can I fix it?
Oh, by the way I use XAMPP, and open the database with phyMyAdmin

SQL script INSERT INTO values unnable to be inserted?

I used a script to make a tables within the same database. Once both tables were made, I made a seperate script to insert a set of values in the table, however with one of the scripts it doesnt seem to insert any data into the table, but comes up with the error.
These are my tables;
CREATE TABLE IF NOT EXISTS customers(
customer_id INT UNSIGNED NOT NULL,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password VARCHAR(40) NOT NULL,
dob DATETIME NOT NULL,
address_line VARCHAR(40) NOT NULL,
postcode VARCHAR(20) NOT NULL,
PRIMARY KEY(customer_id),
FOREIGN KEY(postcode) REFERENCE postcodes(postcode)
);
In a seperate script,
CREATE TABLE IF NOT EXISTS postcodes(
postcode VARCHAR(20) NOT NULL,
address_line_2 VARCHAR(20),
city VARCHAR(40) NOT NULL,
PRIMARY KEY(postcode)
);
The scripts to insert data into the tables are here.
This one works without any errors and successfully populates the table.
INSERT INTO postcodes(postcode,address_line_2,city)
Values
('DH1 568','Forest Lane','Durham'),
('DH1 679','Dry Wood','Durham'),
('DH1 4AS','North Of the Wall','Westeros'),
('DH1 4LA',"Snoop's Crib",'Durham');
And this is the one which comes up with an error message,
INSERT INTO customers(customer_id,first_name,postcode)
values
('1','Zaak','DH1 568'),
('2','Matt','DH1 679'),
('3','Jon','DH1 4AS'),
('4','Zak','DH1 4LA'),
('5','Gaz','DH1 7SO');
The error message which appears is,
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`hardware_store`.`customers`, CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`postcode`) REFER
ENCES `postcodes` (`postcode`))
You have a foreign key constraint specifying that customers(postcode) refers to a valid postal code in the postcodes table.
Then, you try to insert 'DH1 7SO' and it doesn't work because this postcode is not in postcodes.
This is how foreign key references work. The database is working exactly as it should and doing exactly what you instructed it to do.
If you want the valid rows to be inserted and the invalid ones ignored, then use the IGNORE option on INSERT (see here).

I keep getting this mysql error code #1089

CREATE TABLE `movies`.`movie`
( `movie_id` INT(3) NULL AUTO_INCREMENT, `movie_name` VARCHAR(25) NULL,
`movie_embedded_id` VARCHAR(50) NULL, `rating_no` INT(3) NULL,
`movie_description` VARCHAR(50) NULL, PRIMARY KEY (`movie_id`(3))) ENGINE = InnoDB;
I keep getting this error:
#1089 - Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't
support unique prefix keys.
but I've got no idea what it means, anyone have a clue?
With the part
PRIMARY KEY (`movie_id`(3))
you are telling mysql to create a sub part key* on the first 3 Bytes of movie id. This only works for string types.
You need to use
PRIMARY KEY (`movie_id`)
without providing a length.
*Is this sure the query resulting in the error? Never saw that on a primary key, its used for indexes.
after selecting PRIMARY KEY when you create table, don't input any value in pop dialog
You can also get this error when creating an index if you specify a prefix length that is longer than the length of the actual column. If you tried to create an index containing someColumn(20) but in the table someColumn is VARCHAR(15), then this error will occur.
For the primary key do not enter any length of type.
Example:
int()

Members table with one instead of three unique fields

In almost each code example by creating mysql joinUs table, there is the code like this:
CREATE TABLE `members` (
`id` int(10) unsigned NOT NULL auto_increment,
`username` text, //unique
`email` text, //unique
`pass` text,
PRIMARY KEY (`id`)
So, if username and/or email is set as unique, what is then the purpose of id field?
Can I simply set username as primary key and exclude the id from the table?
Id is a nice convention because it will never change. That lets other tables reference users via their id, and enables you to let users change their usernames and emails. Having said that, yes, you could use username as a primary key.

Creating(and deleting entries) in another table automatically

I'm a complete beginner to SQL and I am struggling to find a solution to a pretty simple problem.
The scenario is this: When a user registers on my site they are inserted into a 'users' table with only a few columns(user_id, username, email, password, date).
I don't want to ask anymore of them when they register, however I would like each user to have an optional settings page where they can input additional user info such as forename, surname, bio etc.
To try and keep my database clean I have created a second table called 'user_info' for this info. This table includes the 'user_id' column that exists in 'users'.
What I am struggling with is linking these two tables. I want it so whenever a user is added to the 'users' table a new entry is inserted into 'user_info' with the same 'user_id'. This also needs to work for deletion of users.
So far I have gathered that the use of a FOREIGN KEY is required and have attempted to use CASCADE to solve this.
My tables look like this:
CREATE TABLE users (
user_id INT AUTO_INCREMENT NOT NULL,
username VARCHAR(20) NOT NULL default '',
email VARCHAR(100) NOT NULL default '',
password VARCHAR(100) NOT NULL default '',
create_date DATETIME NOT NULL default '0000-00-00',
PRIMARY KEY (user_id),
UNIQUE KEY (username),
UNIQUE KEY (email)
);
CREATE TABLE user_info (
user_id INT NOT NULL,
forename VARCHAR(30) NOT NULL default '',
surname VARCHAR(30) NOT NULL default '',
dob DATE NOT NULL default '0000-00-00',
location VARCHAR(30) NOT NULL default '',
PRIMARY KEY (user_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE ON UPDATE CASCADE
);
I would appreciate any advice in this issue, along with any advice on general best practice!
Kind Regards
Adam
Take a look at Triggers (http://dev.mysql.com/doc/refman/5.0/en/triggers.html).
I am also just a beginner at sql and the solution provided by me might not be the best. You can deal with this problem in two ways
Finding the last inserted id in your users table and then inserting a blank record in the user_info with the fetched id.
Since your User ID is primary key, simply do this everytime when you insert:
SELECT max(USER_ID) FROM users
store the fetched result in a variable and then insert the value of variable in the user_info table
INSERT INTO user_info (user_id) VALUES ('.$theValueInTHeVariable.');
The second method is by using triggers
in this solution you can make a trigger so that whenever a new id is inserted into the users table it automatically fetches the last generated user_id and insert it into the Users_info table.
for more information on triggers refer to the following sits.
link1
link2
If you still face any problem then please post back.