I am building a survey, in which participants use a html form to enter details.
For example:
What gender are you? Male | Female
The Male/Female option can be selected using a radio button.
However, I would like to store the answer in my database as an INT value, which is 0 for male and 1 for female. How do I write a query which represents that?
I have a rough idea which looks like this, but doesn’t seem to work:
$__g = Array('male' => '0', 'female' => '1');
And also, how do I insert the value into the database? I have this, but am unsure if it works (since I can’t get the representing part correct)
$sql = "INSERT INTO `onlinesurvey`
(gender) VALUES ('".$__g[$_POST['gender']]."');
Many thanks for your help =)
Why convert the string to an int? I think what you want to do here is use an enum column type to represent the range of possible values…
If you use an enum, you would just insert the string literal value as the column value.
EDITED:
Here is some information on how the enum type works:
http://dev.mysql.com/doc/refman/5.0/en/enum.html
So your query would look like this:
EDITED:
I haven’t seen your HTML, but make sure the select list looks something like this:
The critical piece of this is getting the option value attributes to be equal to the enum values.