I have foreign key constraints in the database I'm working on. For example 'type' is a foreign key that may be null.
type CHAR (10) default NULL
The 'type' value is read from an html list as a string. My query works nicely when the submitted value isn't null.
INSERT INTO table (type) VALUES ('$type')
However I have problem when the selected value is null. In that case my query is expanded to something like this:
INSERT INTO table (type) VALUES ('')
Well too bad for me. '' is not null at all. That's an empty string. My query fails because it doesn't satisfy my foreign key constraint.
I see 3 possible solutions but none of them is magic enough for me. Do you have any other idea?
Use a numeric foreign key, etc. I don't want to touch my db design, so that's not an option I like.
Add an empty string record in my type table.
Test the value obtained from the html form. If it's not null, add quotes around it. Submit without quotes, like:
INSERT INTO table (type) VALUES ($type)
Not too bad, but since I quote every value in submit in my queries, I'd like to avoid tricks like that. Maybe that's a bad habit I have?
Thanks for your feedback!