如何将表单上的YES或NO值保存到MySQL数据库

问题描述:


可能重复:



$ b $ to-use-for-storing-boolean-values>使用哪种MySQL数据类型存储布尔值? b

我已经查看了有关此主题的许多问题,但找不到答案,
我有一个包含10个问题的HTML表单,每个问题都有一个单选按钮,可以选择是或否。在数据库中创建表时(使用phpymadmin)(我使用phpymadmin),我需要为每个问题选择的类型是什么,以存储此是否字段?我是一个新手,但是对此答案表示赞赏!

I have viewed many questions on this topic but I can not find an answer, I have a HTML form with 10 questions, each with a radio button for yes or no to be selected. when creating the table in the database (im using phpymadmin) what is the type I need to select for each question to store this yes no field? I am a novice use at this but all answers appreciated!

这完全取决于用例。

一些可能性是:


  • 创建 TINYINT(1)列,将 1 表示是,将 0 表示否;

  • 创建 CHAR(1)列并存储 Y 为yes和 N 否;

  • 创建 VARCHAR(3)CHARSET ascii COLLATE ascii_bin 列并存储'yes'或'否';

  • Create a TINYINT(1) column and store 1 for yes and 0 for no;
  • Create a CHAR(1) column and store Y for yes and N for no;
  • Create a VARCHAR(3) CHARSET ascii COLLATE ascii_bin column and store 'yes' or 'no';

您可以*选择适合自己需求的内容。甚至这些只是少数选择。
真正重要的是选择一个数据并将其作为此类数据的标准。

You are free to choose what fits your needs. And even these are only a few options. What is really important is picking one and sticking to it as a standard for this kind of data.

此外,您可能无需表示 no 的所有值,以防您的数据没有选择为null的选项。您只能设置的值,并将默认值视为

Also, you may not need to represent the no values at all in case your data doesn't have an option to be null. You can only set the yes values and let the default be treated as no.