0

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?

3
  • 1
    You are confusing NULL with empty string. Please read http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html Commented Oct 11, 2011 at 18:03
  • thank you for the reference xint0 Commented Oct 11, 2011 at 18:23
  • i didn't know there was a different between a NULL value and an empty value. I always thought them to be one-in-the-same. Commented Oct 11, 2011 at 18:24

2 Answers 2

1

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.

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

Comments

1

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

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.