在MySQL中存储复选框值

时间:2016-09-26 21:32:28

标签: php html mysql

我有一个html测试,复选框上几乎没有问题。我的目标是将所选值存储在数据库中。

这是我的代码:

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>

我无法将所选复选框存储在数据库中。谁知道什么是错的?非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

//use type="checkbox"
<input name="radio-choice-h-2[]" id="radio-choice-h-2b" value="man" type="checkbox">