douyi1084
2015-12-26 15:49
浏览 152
已采纳

使用JQuery Serialize时如何为空白字段插入NULL值?

I have many forms that I am serializing. The data is posted to either an insert.php page or update.php.

If a form input is left blank, I want to insert a NULL value into my database. I'm using mySQL.

It seems like when a form is serialized, any fields that are left blank are sent as value of '' which does NOT insert as NULL into my db.

I can filter out empty inputs before serializing, but this isn't good when a record needs to be updated.

Here's a fiddle of a form that you can at least see that empty inputs are logged as "".

Any advice on how I can set those values to NULL instead?

jsfiddle.net/gabrieleromanato/bynaK/

图片转代码服务由CSDN问答提供 功能建议

我有很多我要序列化的表单。 数据发布到insert.php页面或update.php。

如果表单输入留空,我想在我的数据库中插入NULL值。 我正在使用mySQL。

似乎当一个表单被序列化时,任何留空的字段都将作为''的值发送,它不会作为NULL插入到我的数据库中。< / p>

我可以在序列化之前过滤掉空输入,但是当需要更新记录时这并不好。

这是一个表单的小提琴,你至少可以看到空输入被记录为“”。

关于如何将这些值设置为NULL的任何建议?

jsfiddle.net/gabrieleromanato/bynaK / < / DIV>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujian1050 2015-12-26 15:57
    已采纳

    You can simply at php side check if value is empty use instead null with strlen(...) == 0 condition ( as @PaulSpiegel mentioned in comment of this answer empty will consider that 0 is also empty and will write null to your db ) - http://php.net/manual/en/function.strlen.php

    // Example
    $value = !strlen($_POST['value']) ? null:$_POST['value'];
    // .. save in MySQL 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题