1
我有一個html測試,其中有幾個問題與複選框。我的目標是將選定的值存儲在數據庫中。在MySQL中存儲複選框值
這裏是我的代碼:
insert.php
<?php
$host="***"; // Host name
$username="***"; // Mysql username
$password="***"; // Mysql password
$db_name="***"; // Database name
$tbl_name="test_submissions"; // Table name
// Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$question1=implode(',',$_POST["radio-choice-h-2"]);
//Instant search results
$perfume1=implode(',', $_POST["product1"]);
$perfume2=implode(',', $_POST["product2"]);
$perfume3=implode(',', $_POST["product3"]);
$noperfume4=implode(',', $_POST["product4"]);
//Rest of questions
$question2=implode(',', $_POST["radio-choice-h-4"]);
$question3=implode(',', $_POST["radio-choice-h-5"]);
$question4=implode(',', $_POST["radio-choice-h-6"]);
$question5=implode(',', $_POST["radio-choice-h-7"]);
$question6=implode(',', $_POST["radio-choice-h-8"]);
$question7=implode(',', $_POST["radio-choice-h-9"]);
$question8=implode(',', $_POST["radio-choice-h-10"]);
$question9=implode(',', $_POST["radio-choice-h-11"]);
$question10=implode(',', $_POST["radio-choice-h-12"]);
$question11=implode(',', $_POST["radio-choice-h-13"]);
$question12=implode(',', $_POST["radio-choice-h-14"]);
$question13=implode(',', $_POST["radio-choice-h-15"]);
$question14=implode(',', $_POST["radio-choice-h-16"]);
//Inserting email
$email=implode(',', $_POST["email"]);
if(isset($_POST['submit']))
{
$sql ="INSERT INTO test_submissions (question1, perfume1, perfume2, perfume3, noperfume5, question2, question3,";
$sql +="question4, question5, question6, question7, question8, question9, question10, question11, question12, question13$
$sql +="question14, question15, mail) VALUES('$question1','$perfume1','$perfume2','$perfume3','$noperfume4','$question2'$
$sql +="'$question5','$question6','$question7','$question8','$question9','$question10','$question11','$question12','$que$
$result=mysql_query($conn, $sql) or die("Error en l'SQL");
}
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
<?php
// close connection
mysql_close();
?>
的index.php
<form action="insert.php" method="post">
<fieldset data-role="controlgroup" data-type="horizontal">
<input name="radio-choice-h-2[]" id="radio-choice-h-2a" value="woman" checked="checked" type="radio">
<label for="radio-choice-h-2a">Mujer</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2b" value="man" type="radio">
<label for="radio-choice-h-2b">Hombre</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2c" value="baby" type="radio">
<label for="radio-choice-h-2c">Bebé</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2d" value="pet" type="radio">
<label for="radio-choice-h-2d">Mascota</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2e" value="environtment" type="radio">
<label for="radio-choice-h-2e">Ambiental</label>
</fieldset>
<input type="submit" name="submit" data-inline="true" value="Submit"></p>
而且我不能夠存儲選中的支票數據庫內的框。任何人都知道什麼是錯的?非常感謝您的幫助!
它們是單選按鈕。一次只能從任何給定的廣播組中選擇一個。他們是'OR'的選擇。如果您想允許多個選擇,請使用複選框(「AND」選項)。那麼,爲什麼你的問題狀態「複選框」,當你的HTML只是「無線電」?另外,請注意,您容易受到[sql注入攻擊](http://bobby-tables.com) –
此架構設計悍然違反[Zero,One或Infinity Rule](http://en.wikipedia。 org/wiki/Zero_one_infinity_rule)[database normalization](http://en.wikipedia.org/wiki/Database_normalization)。 – tadman
**警告**:如果您只是學習PHP,請不要使用['mysql_query'](http://php.net/manual/en/function.mysql-query.php)界面。這是非常可怕和危險的,它在PHP 7中被刪除了。[PDO的替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南介紹了最佳實踐。你的用戶參數是**不是** [正確轉義](http://bobby-tables.com/php),並且有[SQL注入漏洞](http://bobby-tables.com/)可以被利用。 – tadman