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

使用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/

  • 写回答

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 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)