我有3個不同的單選按鈕列表。用戶可以爲每個單選按鈕列表選擇一個值,然後將其存儲到mysql數據庫中。如何將它們存儲在單列,不同的行中?請幫忙!謝謝。如何將單選按鈕列表值存儲到單列,不同行的mysql中?
//sql
$sql = "INSERT into ratings (product, rating) VALUES ('".$key."', '".."')";
$result = $mysqli->query($sql);
我有3個不同的單選按鈕列表。用戶可以爲每個單選按鈕列表選擇一個值,然後將其存儲到mysql數據庫中。如何將它們存儲在單列,不同的行中?請幫忙!謝謝。如何將單選按鈕列表值存儲到單列,不同行的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);
要存儲單個或多個選擇,作爲一個記錄(列),我建議編碼的選擇作爲一個整數值。
如果您有許多獨佔選項,您可以將它們編碼爲整數值。
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
這是http://stackoverflow.com/questions/14230770/how-do-store-3-radio-button-lists-in-mysql-database-in-single-column-different的副本 – artsylar