tags:

views:

73

answers:

4

Hello,

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

A: 

You can just use the value attribute.

<select id="gender" name="gender">
  <option value="0">male</option>
  <option value="1">female</option>
</select>

Although, MySQL has better data types than int for this. I would recommend a boolean type.

danben
The MySQL "BOOL" type maps to TINYINT.
Charles
I would need INT because there are some questions that require more than 2 choices. Thanks!
Tim
enum is more efficient, and much clearer. gender = 0/1 which is not obvious, except the (perhaps not)obvious lol. (male=1 female=0) tinyint can have 127 positive values, int on most machines will have 4Billion possible values http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
DeveloperChris
A: 

How do I write a query which represents that?

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

And also, how do I insert the value into the database?

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:

$sql = "INSERT INTO `onlinesurvey` (gender) VALUES ($_POST['gender']);

EDITED:

I haven't seen your HTML, but make sure the select list looks something like this:

<select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>

The critical piece of this is getting the option value attributes to be equal to the enum values.

AJ
Hello, assuming I do not want to convert values, how do I insert it as "male/female", based on user's input? How do I improve on this?$male = ($__g[$_POST['gender']] == 'male')$female = ($__g[$_POST['gender']] == 'female')$sql = "INSERT INTO `onlinesurvey` (gender) VALUES ('".$__g[$_POST['gender']]."');Once again, many thanks.
Tim
@Tim: edited my answer to give you a link to how enum works, and also to show you the modified form of the query.
AJ
@AJ, I have added an enum column titled "gender" with values allowed "'male',female'" and added the line $sql = "INSERT INTO `onlinesurvey` (gender) VALUES ($_POST['gender']);. Is there anything else I need to do to ensure that the values get added in? right now there's no error, but the values are not getting entered into the database.
Tim
@Tim, edited answer again to show the relationship between the select list and the enum. Feel free to send me your PHP and I'd be happy to review it. You can contact me at "aj DOT coon AT gmail DOT com".
AJ
A: 

What danben said.

You should also make sure to store all these associates in the DB, mainly for record keeping, lookup info about the enum field.

One more thing, INT is overkill use TINYINT

MindStalker
+1  A: 

MySQL ENUM data type is the best choice for this kind of stuff.

CREATE TABLE survey (
    gender ENUM('Male', 'Female', 'Hemaphrodite')
);

INSERT INTO survey VALUES ('Female'); // will get index 1
INSERT INTO survey VALUES ('Hemaphrodite'); // will get index 2
INSERT INTO survey VALUES ('Male'); // will get index 0
Alix Axel