In my application in-spite of the strict validation on client and server side both, somehow it's happen that record is going blank from user registration page. All data is blank accept the default value in my table (eg. Gender - Male). This happens approximately 1 in 100 registration.
Mysql - I have structure like not null for every required field. Still it's inserting blank.
Cakephp side - I am checking data array is blank or not before saving it.
开发者_JS百科I will really appreciate if anyone help me out.
Thanks in advance.
It's blank because in every database (except Oracle) an empty string ''
is not null. Therefore the database will accept that.
If you want to prevent this from happening you need to write a trigger like so:
DELIMITER $$
CREATE TRIGGER bi_table1_each BEFORE INSERT ON table1 FOR EACH ROW
BEGIN
//this will cause an error and prevent the insertion of an empty gender field.
//If gender has a default value, the database will insert the default value instead.
IF new.gender = '' THEN SET new.gender = null;
END $$
DELIMITER ;
Alternatively you can make gender a enum field
ALTER table1
CHANGE COLUMN gender gender ENUM ('M','F');
Links:
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://dev.mysql.com/doc/refman/5.5/en/triggers.html
精彩评论