【问题标题】:How do store 3 radio button list values into mysql in a single column, different row?如何将 3 个单选按钮列表值存储到 mysql 的单列不同行中?
【发布时间】:2012-12-23 04:50:09
【问题描述】:

我有 3 个不同的单选按钮列表。用户可以为每个单选按钮列表选择一个值,然后将其存储到mysql数据库中。如何将它们存储在单列、不同的行中?请帮忙!谢谢。

//sql

$sql = "INSERT into ratings (product, rating) VALUES ('".$key."', '".."')";
$result = $mysqli->query($sql);

【问题讨论】:

标签: mysql database list button radio


【解决方案1】:

要将单个或多个选项存储为单个记录(列),我建议将选项编码为整数值。

如果您有许多独占的选项,您可以将它们编码为整数值。

option1 encoded as 1
option2 encoded as 2
option3 encoded as 3
option4 encoded as 4
option5 encoded as 5

如果您只有很少且非排他性的选项,您应该将它们编码为整数中的位:

option1 encoded as 1 (binary ...00001)
option2 encoded as 2 (binary ...00010)
option3 encoded as 4 (binary ...00100)
option4 encoded as 8 (binary ...01000)

第二种表示允许您在单个整数值中存储 2 个或更多选择:

option2 + opton4 encoded as 8 + 2 = 10 (binary ...01010)
(no option selected) encoded as 0      (binary ...00000)

有关 bitvise 操作的更多信息: http://en.wikipedia.org/wiki/Bitwise_operation

【讨论】:

    【解决方案2】:

    对多插入行使用 INSERT 语句。

    INSERT INTO ratings (product, rating) 
    VALUES (1, 1), 
           (1, 2), 
           (1, 3);
    

    查看此链接MySQL INSERT Statement

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 2015-09-13
      相关资源
      最近更新 更多