当前位置: 动力学知识库 > 问答 > 编程问答 >

What mysql data type for radio buttons?

问题描述:

I'm making a form with radio buttons (see http://jsfiddle.net/mjmitche/3c6Mc/) and users are submitting data mostly through radio buttons. What "type" will the values for the radio buttons have to be/should they be in the mysql table?

The fields advocacy, drafting, interview etc will all be either "pass" or "fail" on the webform, so what do I do in the database?

CREATE TABLE `shoutbox`(

`id` int(5) NOT NULL auto_increment,

`date` timestamp NOT NULL default CURRENT_TIMESTAMP,

`instructor` varchar(25) NOT NULL ,

`secretmessage` varchar(255) NOT NULL default '',

`advocacy` NOT NULL,

`drafting` NOT NULL,

`interview`

`letter`

`solicitor`

`barrister`

PRIMARY KEY (`id`)

);

Boolean

If I use a Boolean, do I set the values for Pass and Fail to 1 and 0?

 <tr>

<td><label>Interview</label></td>

<td><input type="radio" name="interview" value="1" /> Pass</td>

<td><input type="radio" name="interview" value="0" /> Fail</td>

</tr>

with this mysql?

 CREATE TABLE `shoutbox`(

`interview` BOOLEAN NOT NULL,

TinyINT

If I use a TinyInt, do I also set the value of Pass and Fail to 1 and 0?

 <tr>

<td><label>Interview</label></td>

<td><input type="radio" name="interview" value="1" /> Pass</td>

<td><input type="radio" name="interview" value="0" /> Fail</td>

</tr>

CREATE TABLE `shoutbox`(

`interview` TINYINT NOT NULL,

ENUM

If I use enum, do I set the value to "pass" and "fail" and then use the same names in the database with enum?

 <td><label>Interview</label></td>

<td><input type="radio" name="interview" value="pass" /> Pass</td>

<td><input type="radio" name="interview" value="fail" /> Fail</td>

</tr>

Mysql

 CREATE TABLE `shoutbox`(

`interview` enum('pass', 'fail') NOT NULL,

网友答案:

ENUM may be behave unexpectedly if inserting integer values.

Example:

CREATE TABLE `shoutbox`(  
`interview` enum('pass', 'fail') NOT NULL);

INSERT INTO shoutbox (0); // this would be equivalent to an empty ENUM value ('')
INSERT INTO shoutbox (1); // represents 'pass', 2 represents 'fail'
INSERT INTO shoutbox ('pass'); // works as expected, same for 'fail'.

If you used ENUM, it would be best to validate the radio input value so that no empty or invalid strings are passed.

$interview = in_array($_POST['interview'], array('pass', 'fail')) ? $_POST['interview'] : 'fail';

I recommend using an BOOLEAN or TINYINT and validating the radio input value by using intval();

CREATE TABLE `shoutbox`(  
`interview` tinyint(1) NOT NULL);

php:

$interview = intval($_POST['interview']); // sets interview to 0 or 1
// "insert into shoutbox($interview);"
网友答案:

In general, enum type would back a radio set best, but since your options are pass / fail, you could use bool.

网友答案:

You should think of this in terms of what the radio buttons represent in the real world.

The answer is probably that they represent a short list of mutually exclusive things. Generally those are represented in a business object with an enum, which can be saved most space-efficiently as a tinyint in the database.

A [tinyint][1] can represent values from -128 to 127, so works for up to 256 different values (assuming your language can map enum values to negative numbers) or up to 128 different values if your enums are restricted to non-negative numbers. If you have more different values (which would make for a hard-to-use UI), you could step up to a smallint.

For things that only have two values, you still won't get more efficient than using a tinyint (short of mapping multiple questions into different bit positions of a numeric field, which I strongly discourage under all but the most extreme conditions). MySQL does support a [bit data type][2] that supports bit fields, but I doubt that bit(1) would still not use less storage than tinyint (it should use the same amount, but I could not find a specific reference).

EDIT:

Regarding your edit asking about how to use the MySQL enum datatype, I actually do not recommend it's use if you have a business logic layer. The reason for that is that you need to maintain the enum definition in two places (the business object and the DDL for MySQL). So, I would go with your Tinyint example. I would also have the business layer generate the values into your UI rather than hard coding them.

Using the Boolean data type is clearer if you genuinely have a Boolean condition. However, right now you have a list of values that will "only ever have two values". Experience shows that truth will change during the lifetime of your program more often than you might expect (maybe "Pass" and "Fail" gets additional values like "Incomplete Interview" or whatever).

分享给朋友:
您可能感兴趣的文章:
随机阅读: