php - Handling NaN in a Postgresql database - Stack Overflow

I have a simple web-survey where users can choose their answers using radio buttons. I check which butt

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
Add a ment  | 

1 Answer 1

Reset to default 4

On 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851922a4597174.html

相关推荐

  • php - Handling NaN in a Postgresql database - Stack Overflow

    I have a simple web-survey where users can choose their answers using radio buttons. I check which butt

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信