如果语句为
问题描述:
从NULL插入NULL到MySql如果它是0.00
,我使用if语句将变量赋值为数值或向其写入NULL。如果语句为
当从我的PHP页面写入MySQL时,我试图传递此值,并且它始终为NULL
写入0.00
。我猜这是因为我的插入语句发送.$price.
,它认为它在引号中?
如果我在插入之前回显$price
,它会告诉我它是NULL
,但在插入语句之后,它永远不会保留。
如何插入NULL
..因为我必须使用一个变量并发送一个值(如果有的话)?
答
要插入NULL
到您的表中,您需要将字符串"NULL"
传递到您的查询中,而不是PHP NULL
的值。一个PHP NULL
,如果用单引号括起来并传递给查询,将被解释为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);
这之前...
+0
THANK YOU !!!我知道了。 :) – Andi
答
很难说,你不要发布任何定义或代码,但可能你的表不允许NULL,等等它默认为该类型的“零”值。
'field_name' = null – EKet