开发者

INSERT NULL if a field is left blank (decimal) MySQL/PHP

I have a if NULL echo "Message" else $value.

There is some JavaScript hiding the input field, so they check it and enter the value.

If they don't check it to enter the field, the db enters 0.00 and doesn't specify NULL so my PHP if开发者_StackOverflow中文版 statement works.

How do I either set the variable as NULL if its blank, or set NULL in the INSERT statement?

`myTableField` decimal(10,2) default NULL,


Not sure exactly what you're after, but you can run the below if statement to check what value was entered, and set to null accordingly:

if(!is_numeric($fieldvalue) || $fieldvalue==0){
  // if the entered value isnt a number (i.e. isnt entered, or invalid) or if the value is zero, sounded like it was your default
  $fieldvalue=NULL;
  // could also use unset($fieldvalue);
}

If the variable $fieldvalue is set to null (or un-set), it will be inserted as a NULL in your DB according to your field definitions. Make sure your insert statement references the value without ' or " encapsulating figures however (not needed as its a decimal field).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜