admin管理员组文章数量:1404927
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) );
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) );
Share
Improve this question
asked Apr 4, 2012 at 16:22
djqdjq
15.3k46 gold badges126 silver badges158 bronze badges
1
-
NaN
is a Javascript thing. Whatever you are doing in Javascript is making the values that get passed in NaN instead of null. PHP doesn't care if the parameters are null, and your database probably doesn't either. – AndrewR Commented Apr 4, 2012 at 16:30
1 Answer
Reset to default 4On solution would be to change that query to:
'INSERT INTO tracking.postsurvey(answer1) values(nullif($1, 'NaN'))'
http://www.postgresql/docs/current/interactive/functions-conditional.html#FUNCTIONS-NULLIF
本文标签: phpHandling NaN in a Postgresql databaseStack Overflow
版权声明:本文标题:php - Handling NaN in a Postgresql database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744851921a2628542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论