drqn5418 2011-09-26 02:48
浏览 83
已采纳

将是/否单选按钮存储到MySQL数据库中

I've seen a Yes/No form radio buttons value be stored/saved in a couple different ways. I wonder which way is better and why? This is for a PHP/MySQL application with a typical Yes/No question as part of a form.

1.) Store it as 1, 0 or null. 1 being Yes, 0 being No and null being not answered.

2.) Store it as Yes, No, null. Assume a language conversion can be made.

3.) Use 1, 2 and null so as to better distinct the values.

Thanks, Jeff

Edit: I also must mention that most of the issues have been arising due to jQuery/JavaScript and the comparisons and $() bindings.

  • 写回答

6条回答 默认 最新

  • doujiu8178 2011-09-26 02:54
    关注

    Since MySQL has a BOOLEAN type, but it's simply an alias of TINYINT. I recommend against it because the equal sign in PHP == would not distinguish 0 from the lack of value. You'd always need to use triple equal === and it would be easy to make mistakes.

    As for your options:

    1. This seems the natural choice with PHP, but then you've to be careful to distinguish 0 from the lack of value, so I wouldn't recommend it.

    2. I would not recommend this one.

    3. Possible, but the assignment to 1 and 2 is somewhat arbitrary and might be difficult to remember and read in code.

    What I usually do, is use "Y", "N" and NULL if needed, in a CHAR(1) field, it reads well in code and doesn't create problems.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题