Problem with inserting empty fields into mysql database
Hi,
I have a registration form built in dreamweaver designed to insert data into my database through PHP, with some of the fields in my form optional to the user.
My understanding is that I should set these 'optional fields' to NULL in mysql in order to be able to proceed with the registration should the user choose not to enter any data. This appears to work fine and the website moves on as required upon the form being submitted.
However, should the user choose to enter something into one of these fields marked NULL in mysql the data is lost and when I look at the inserted row it simply says NULL, regardless of what data the user has entered in the form.
If I go the other way and mark the field NOT NULL then the data appears correctly, only for the user to get an error message saying 'column cannot be null' should they decide to leave it blank.
I am a relative newbie to the world of PHP and mysql so am probably missing something simple. Any help would be much appreciated.
Can't say without seeing code, but the issue is most likely a PHP issue, not so much of an SQL issue.
Yes, by marking ALLOW NULL in those fields in the database, you can leave the value blank (same thing if you default the column value to ''). But if data is entered into the form field but it's not being updated in the database, I'm guessing the problem is in PHP not updating that column in the database.
I'm not that versed in PHP, I've barely started learning it.
My guess is that it most likely has something to do with the ternary conditionals you are using. Plus, you are encapsulating NULL in quotes, which makes me think that "NULL" is actually being entered as the value, not that the column is null for that record.
Post your question and sample code in the PHP forum, here. Sorry I can't be of more assistance.
Bookmarks