Anthony Miller
Anthony Miller

Reputation: 15920

PHP Forms data to MYSQL Database NOT NULL rule

I'm messing around with PHP and a test MYSQL database. I've created a table called 'names' with columns firstname and lastname; both are set as NOT NULL. However, I create a PHP form to enter in a first and last name, then it sends the data to the appropriate table fields via the $_POST method. If I enter no information for both fields, the database still accepts the input and creates the row with no data in each field.

Why should I assign NOT NULL to columns if I'm going to have to put checks in place for null entries in the PHP form?

Upvotes: 0

Views: 396

Answers (2)

Kibbee
Kibbee

Reputation: 66132

In this case you database is storing empty string, and not the "null" value. Empty string and "null" are very different values as far as the database is concerned. If you want to require a not-empty value, you should add additional constraints to your table. Also, it's worth checking the constraints (or handling the resulting errors) in your PHP code so that the user gets a useful message and can submit proper information. The constraints in the database are just a last-ditch effort to ensure that no data which does not pass the constraints gets in.

Upvotes: 1

Aaron Dougherty
Aaron Dougherty

Reputation: 725

It sounds like the values in the database are not in fact NULL, possibly they are blank?

Upvotes: 1

Related Questions