Jump to content



Photo

False Inserts Into A Db


  • Please log in to reply
1 reply to this topic

#1 Paul

Paul

    Advanced Member

  • Members
  • PipPipPip
  • 130 posts
  • LocationCumbria, UK

Posted 10 March 2012 - 7:38 AM

Hi all,
I have a form that a user fills in to register themselves. I start by making the form variables all FALSE. I then do all the checks, including that none of the variables are still FALSE, and then an INSERT into MySQL.

Now 2 of the form fields are not mandatory, i.e. they can be left blank by the user. So, at this stage they will still be set at FALSE. It won't pass the test so won't INSERT.

I then removed these two variables from the if($blah && $blah etc.) check. It worked and the INSERT worked as well. I then checked the DB table and the 2 non-mandatory fields that had no entry in them appear in the DB as blank fields.

Now to the question (and a typical beginners question with regard to FALSE, NULL, 'blank?'). Are they really 'blank', i.e. empty? When I come to do SELECT queries later on and include those fields am I heading for trouble. What will come back? Would it be better to set the non-mandatory form variables to an empty string, i.e. $blah = ''; if the user did not use them. Then the if... test would work (none would be FALSE) and the DB would get an empty string insrted instead of FALSE. Does it make any difference?

Cheers
Paul

PS. How can I set up this forum so that it emails me all the posts, instead of me having to log in?
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,550 posts
  • LocationState College, PA (USA)

Posted 12 March 2012 - 8:01 AM

Good question. The short answer is that a FALSE value in a PHP variable is not the same as a MySQL FALSE or NULL. If you assign the value false to a PHP variable, the resulting database value for that column will depend upon the column's type. It could be 0 for an integer column or an empty string for a character/text column. This would be a problem if later queries would check for that column having a NULL value, because it wouldn't.

What I do in these situations is assign the value NULL to optional fields that the user didn't fill out. You have to be careful with the syntax, though, because the resulting query needs to insert for that column the word NULL without quotes for this to work.

As for getting emails, I believe there's a checkbox for that option when you create the thread.
  • 0