我有 3 个不同的单选按钮列表。用户可以为每个单选按钮列表选择一个值,然后将其存储到mysql数据库中。如何将它们存储在单列、不同的行中?请帮忙!谢谢。
//sql
$sql = "INSERT into ratings (product, rating) VALUES ('".$key."', '".."')";
$result = $mysqli->query($sql);
对多插入行使用 INSERT 语句。
INSERT INTO ratings (product, rating)
VALUES (1, 1),
(1, 2),
(1, 3);
检查此链接MySQL INSERT 语句
要将单个或多个选项存储为单个记录(列),我建议将选项编码为整数值。
如果您有许多独占选项,则可以将它们编码为整数值。
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