使用PHP通过复选框输入类型在数据库中插入多个用户选择

时间:2018-03-23 10:28:00

标签: php mysql

我正在开发一个网站,其形式使用户可以通过输入类型选择多个选项&#34; Checkbox&#34;,我已经堆积了如何在数据库中插入多个用户选择.. < / p>

这是我的代码:

if(isset($_GET['add']) || isset($_GET['edit'])){
  $fullname = ((isset($_POST['fullname']))? clean($_POST['fullname']):'');
  $win = ((isset($_POST['win']))? clean($_POST['win']):'');
  $nominated = ((isset($_POST['nominated']))? clean($_POST['nominated']):'');
  $bio = ((isset($_POST['bio']))? clean($_POST['bio']):'');
  $movies = ((isset($_POST['movies']))? clean($_POST['movies']):'');
  $birth = ((isset($_POST['birth']))? clean($_POST['birth']):'');

$insertSQL="INSERT INTO crew (fullname,win,nominated,image,movies,bio,birth)


        VALUES('$fullname','$win','$nominated','$dbpath','$movies','$bio','$birth')";
           $_SESSION['success']= 'Crew Member Added successfully';
           header('Location: crew.php');

    $db->query($insertSQL); 

     <div class="form-group col-md-3">
            <label for="movies">MOVIES YOU CONTRIBUTED IN:</label><br>
                <?php while ($movies = mysqli_fetch_assoc($sql)) { ?>
                <input type="checkbox" name="movies" value="<?=$movies['title'];?>"><?=$movies['title'];?><br>
                <?php } ?>
                </div>
        <div class="form-group col-md-6">
                        <label for="awards">Awards Winning:</label>
                            <?php while ($winning = mysqli_fetch_assoc($sql1)) { ?>
                        <input type="checkbox" name="win" value="<?=$winning['name'];?>"><?=$winning['name'];?><br>
                         <?php 

} ?>
                 </div> 

   <div class="form-group col-md-6">
                    <label for="awards">Awards Nominated:</label>
                        <?php while ($nom = mysqli_fetch_assoc($sql2)) {     ?>
                    <input type="checkbox" name="nominated" value="<?=$nom['name'];?>"><?=$nom['name'];?><br>
                    <?php } ?>
                 </div>

1 个答案:

答案 0 :(得分:1)

<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
            echo $check; //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}
?>

将表单中的名称设置为check_list [],您将能够以阵列形式访问所有复选框($ _ POST ['check_list'] [])。