简体   繁体   中英

How to insert selected checkbox data from dropdown list in MYSQL database with PHP

I have a registration form in which user can select multiple option through checkbox. But now when I try to insert selected data into database, its not working. Here are some codes:

HTML CODE :

 <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>

The JS

<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>

AND THE PHP: This the code to insert data into the database. I'm using old php version for inserting data. Just to practice.

<?php
if(isset($_POST['submit'])){
include("test/admin/includes/db.php");    
$skills=$_POST['langOpt2'];

foreach($_POST['langOpt2'] as $skills){
mysql_query("insert into `job_seeker_reg` (`j_skills`)  values('','$skills')");
}}
?>

Bellow code help for solving your problem. I got an example is select the subjects. you can use options menu for doing your task.

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 code:

<?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........."; 
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM