J. Doe J. Doe - 1 year ago 63
MySQL Question

How can I have a column of Radio buttons in Mysql Table

After much googling to no avail, I am hoping someone can help me figure out how I can create a table of some information where the last column is just a column of radio buttons for each row. Something Like this:

ID | First Name | last Name |Select
1 | John | Smith | O (radio button)

I basically want user to be able to see the table on their screen and be able to select certain rows using the radio buttons and perform some sort of function like viewing a new table with just that row. I just need help with creating the last column.

Answer Source

MySQL doesn't store "radio buttons". That's not a datatype.

Think about what a "radio button" is. How does the state of a "radio button" get set on display, and get passed back in a form?

A "radio button" has a property "checked", which is either TRUE or FALSE.

If you want to store a "radio button" for a row in a database table, what you really want to store is the state of "checked" property... either TRUE or FALSE.

The easiest way to do that in MySQL is to define a column. Give the column a meaningful name, but for sake of illustration...

 radio_button_state TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'boolean'

A value of 0 is equivalent to FALSE. Any other non-NULL value would be equivalent to TRUE. (And if your column allows for NULL values, a NULL would just be NULL (which is neither TRUE or FALSE.)

There are lots of other ways to implement boolean values in a database column.

Some developers like to store character strings, like 'Y'/'N' or 'T'/'F'. Some developers argue for storing binary types like BIT.

But for maximum ease of use and portability, an integer type is the way to go.

Again, in terms of evaluating the value of the column in SQL...

  • a value of 0 is FALSE.
  • a value of 1 is TRUE
  • any non-NULL value other than zero is also TRUE
  • a NULL is just NULL (never TRUE or FALSE)