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

    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败