douer9399 2012-04-04 16:22
浏览 398

在Postgresql数据库中处理NaN

I have a simple web-survey where users can choose their answers using radio buttons. I check which buttons have been ticked using javascript. I am not setting the buttons to any default state, so it is possible not to answer for questions.

When I pass this to php and try inserting the results into columns of type integer, if all the radio buttons are ticked there are no problems. However, if there are any un-ticked buttons I get the following error message:

Query failed: ERROR:  invalid input syntax for integer: "NaN"

I understand that this is due to the fact that my integer columns cannot handle a string, but I don't know what the best strategy for handling this is. Should I check on the javascript side or is there any approach in php where I should be testing that the values are not null?

I am inserting the results into my database using the following structure:

$query=pg_query_params( $connection, 'INSERT INTO tracking.postsurvey(answer1) values($1)', array($answer1) );
  • 写回答

1条回答 默认 最新

  • douzhang2092 2012-04-04 17:09
    关注

    On solution would be to change that query to:

    'INSERT INTO tracking.postsurvey(answer1) values(nullif($1, 'NaN'))'
    

    http://www.postgresql.org/docs/current/interactive/functions-conditional.html#FUNCTIONS-NULLIF

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题