如果变量为0.00
,我将为其赋值,或者使用if语句将NULL写入该变量。
当从我的PHP页面写入MySQL时,我试图传递这个值,它总是为NULL
写0.00
。我猜这是因为我的insert语句正在发送.$price.
,而它认为它是在引号中?
如果我在insert语句之前回显$price
,它会告诉我它是NULL
,但是在insert语句之后,它再也不存在了。
如何插入NULL
..因为我必须使用一个变量并发送一个值(如果有)?
发布于 2011-08-22 20:26:21
要将NULL
插入到表中,实际上需要将字符串"NULL"
传递到查询中,而不是传递PHP值。如果用单引号括起来并传递给查询,则PHP将被解释为0
。
// If $price is NULL in PHP, set it to the string "NULL"
// Otherwise, set it to the current value of $price, but enclosed in single quotes as '$price'
$price = $price === NULL ? "NULL" : "'$price'";
// Then $price gets passed to your query either as NULL or as the single-quoted $price
mysql_query("INSERT INTO tbl (price) VALUES ($price);");
当然,在此之前您已经调用过$price = mysql_real_escape_string($price);
...
发布于 2011-08-22 20:26:46
很难说,因为您没有发布任何定义或代码,但可能您的表不允许NULL,因此它默认为类型的“零”值。
https://stackoverflow.com/questions/7153162
复制