Disallow MySQL to accept Empty string

Codes here !

Moderators: macek, egami, gesf

jplus2
New php-forum User
New php-forum User
Posts: 87
Joined: Wed May 16, 2012 4:17 pm

Disallow MySQL to accept Empty string

Postby jplus2 » Tue May 22, 2012 9:37 pm

I found out that even if you declare a column in a table in mysql as NOT NULL
it would still allow you to insert an empty string. ''.

how can I prevent this from happening in mysql?
I can always make the check in php, but, I want it to be in both ends.

Thank you in advance guys

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Disallow MySQL to accept Empty string

Postby Nullsig » Wed May 23, 2012 7:26 am

Technically you could do it with a trigger.

You would need to execute it in MySQL console or in PHPMyAdmin

Code: Select all

CREATE TRIGGER <name of the trigger> BEFORE INSERT ON <name of table>
FOR EACH ROW BEGIN
  IF NEW.<fieldname> = ''
  THEN
    SET NEW='Error: Cannot insert record empty value for <fieldname>';
  END IF;
END
;



Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests

cron