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
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

本文标签: phpHandling NaN in a Postgresql databaseStack Overflow