如何使用PHP在MYSQL数据库的下拉列表中插入选定的复选框数据

时间:2017-08-27 09:39:13

标签: php jquery mysql

我有一个注册表单,用户可以通过复选框选择多个选项。但是现在当我尝试将所选数据插入数据库时​​,它无法正常工作。以下是一些代码:

HTML CODE

app.post("/api/posts", function(req, res) {
  console.log(req.body);
  db.Post.create({
    image: req.body.image,
    caption: req.body.caption,
    tags: req.body.tags
  }).then(function(dbPost) {
    res.redirect('/');
    console.log("posted!")
  }).catch(function(error) {
    // Handle the error here
    console.error(error);
  });

JS

 <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="decisions3">Skills</label>
                                       <select name="langOpt2[]" id="langOpt2"   multiple="multiple" class="form-control" required data-validation-required-messge="This field is required">



                                            <?php $selectskill = 'select * from skills where status=1';
                                            $dataskill = mysql_query($selectskill);
                                             while($resultskill = mysql_fetch_object($dataskill))
                                             {?>
                                              <option value="<?=$resultskill->skill_name?>"<?php if($result001->skill_name==$resultskill->skill_name){?> selected="selected"<?php } ?> >
                                              <?=$resultskill->skill_name?>
                                             </option>
                                             <?php }
                                             ?>
                                        </select>
                                    </div>

和PHP: 这是将数据插入数据库的代码。我使用旧的php版本插入数据。只是练习。

<script>
$('#langOpt').multiselect({
   columns: 1,
   placeholder: 'Select Languages'
});

$('#langOpt2').multiselect({
    columns: 1,
    placeholder: 'Select Languages',
    search: true
});

$('#langOpt3').multiselect({
    columns: 1,
    placeholder: 'Select Languages',
    search: true,
    selectAll: true
});

$('#langOptgroup').multiselect({
    columns: 4,
    placeholder: 'Select Languages',
    search: true,
    selectAll: true
});
</script>

1 个答案:

答案 0 :(得分:0)

Bellow代码有助于解决您的问题。我举了一个例子就是选择主题。您可以使用选项菜单来完成任务。

HTML code:

select subject: <select name="Subject" id="dob-day">
        <option ">-----</option>
		<option value="Maths">Maths</option>
		<option value="Science">Science</option>
   			<option value="Computer Science">Computer Science</option>
		<option value="Languages">English</option>
		<option value="Others">Others</option>
</select>

php代码:

<?php
$dbhost = "localhost";// enter host
$dbuser = "root";// enter root
$dbpass = ""; // enter password
$dberror1 = "Could not connect to the database connection";
$dberror2 = "Could not connect to the database";

$conn = mysqli_connect($dbhost,$dbuser,$dbpass) or die ($dberror1);
$select_db = mysqli_select_db($conn,'your_database_name') or die ($dberror2);

$subject   = $_POST['Subject'];
$sql = "INSERT INTO your_table_name (Subject) VALUES ('$subject')";

$result= mysqli_query($conn,$sql);
if($result){
    echo "Enter is success........."; 
}
?>