2

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.

I will really appreciate if anyone help me out.

Thanks in advance.

1
  • Can you post your validation rules? Commented Jul 14, 2011 at 14:13

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.