【发布时间】:2011-04-15 21:27:28
【问题描述】:
我的页面使用 ajax 数据发送到 php 脚本。脚本获取的变量之一是:$product_disc 现在,我在脚本中完成的一个小测试清楚地表明 $product_disc 为空。测试:
if ($product_disc == null)
{$test="null";}
else {$test="not null";}
我让我的代码将 $test 返回到 ajax 调用过程:
$return_array['success'] = array ('test' => $test);
使用 Firebug 我看到 ajax 响应有:“test”:“null”
所以我得出结论 $product_disc 是有价值的:null
现在,在我的脚本中,我使用以下代码将数据插入 MySql DB:
$stmt = mysqli_prepare($link, "INSERT INTO set_products (id,discount) VALUES (NULL , ?)");
mysqli_stmt_bind_param($stmt, "i", $product_disc);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
查询正在执行,但是插入的值是0而不是NULL!
只有当我明确添加: $product_disc=null;在 MySqli 语句之前,插入的值为 NULL。
这是为什么呢?空值和空值有什么区别?我在*中关注了这个answer,希望我可以轻松插入NULL,但后来出现了这个问题......
谢谢!
PS @* 团队 - 您的拼写检查字典无法识别单词 *!
【问题讨论】:
-
你准备 product_disc 为整数,null 不是整数。它被转换为 0
-
哦.. 将 i 更改为 s 解决了它:) 为什么不将其作为答案发布?
-
@Israel:拼写检查器在您的浏览器中,哈哈)
-
@Israel 完成并提供了小费。
-
@Khez - 嘿,但是为什么当我明确地将 var 声明为 NULL 时,它的行为会有所不同?在这两种方式中 var 都是 null,但一种方式 t 被转换为整数,另一种方式不是?!