0

如何使用复选框在数据库中插入选择选项值?当我单击复选框并单击注册按钮时,选择选项不会插入所选值。我在“examtype”表中有 3 个选项(常规、重考、追索权)但是当我选择更多时不止一个主题选择选项无法正常工作。这是我的代码... 这是我的选择选项图片

    <?php
if (isset($_POST['enroll']))
{
if (!empty($_POST['chk1'])) 
{
  if (isset($_POST['et']))
  {
    $roll_no = $_SESSION['roll_no'];
    //$selectbox1 = $_POST['et'];
  
foreach($_POST['chk1'] as $checkbox1)
{
foreach($_POST['et'] as $selectbox1)
//for($i=0;$i<sizeof($selectbox1);$i++)

$values = explode("|" , $checkbox1);
 $values1= explode("|" , $selectbox1);
$course_id= $values[0];
$semester= $values[1];
$course_name= $values[2];
$selectbox1=$values1[0];

$sql="INSERT INTO pendingcourse(roll_no,course_id,semester,course_name,exam_type,status) VALUES('$roll_no','$course_id','$semester','$course_name','$selectbox1',0)";
$stmt = $connect->prepare($sql);
$stmt->execute();
$checkbox1='';
$selectbox1='';
}
header("location:coursetable.php");
}
}
}
?>



 <form action="coursetable.php" method="post" enctype="multipart/form-data"> 
<div class="container mt-3" >
    <div class="accordion mb-4" id="accordionExample" >
        <div class="accordion-item" style="background-color: rgb(0, 225, 255);">
          <h2 class="accordion-header" id="headingOne">
            <button class="accordion-button collapsed fw-bold" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne" style="background: #AAFFA9;">
              1st Semester
            </button>
          </h2>
          <div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" >
            <div class="accordion-body">
                <table class="table table-bordered border-primary text-center">
                    <thead>
                      <tr>
                        <th scope="col-1">Course Id</th>
                        <th scope="col-1">Semester</th>
                        <th scope="col-7">Course</th>
                        <th scope="col-1">Exam Type</th>
                        <th scope="col-1">Select</th>
                        <th scope="col-2">Result</th>
                      </tr>
                    </thead>
                    <tbody>
                    
                 
<?php
$stmt = $connect->query("SELECT course_id,semester,course_name FROM coursetable Where semester = '1st' ");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
  $course_id = $row['course_id'];
  $semester = $row['semester'];
  $course_name = $row['course_name'];

?>
                    <tr>
                        <td scope="row"> <?php echo $course_id?></td>
                        <td > <?php echo $semester ?></td>
                        <td ><?php echo $course_name ?></td>
                        <td>

                        <select name="et[]"  class="form-select form-select-sm" aria-label=".form-select-sm example">
                        <?php 
                              $stmt1 = $connect->query("SELECT * from examtype");
                              $rows1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
                              foreach($rows1 as $row1) {
                         ?>
                              <option value="<?php echo $row1['exam_type']; ?>"><?php echo $row1['exam_type']; ?></option>
                         <?php 
                            }
                          ?>
                        </select>
                        </td>  
                        <td>     
                        <input type="checkbox" name="chk1[]" value="<?php echo $row['course_id']?>|<?php echo $row['semester']?>|<?php echo $row['course_name']?>|<?php echo $row1['exam_type']?>">
                            <label class="form-check-label" for="flexCheckDefault">
                            </label>  
                        </td>
                        <td></td>
                      </tr>                     
 <?php
}
?>
                    </tbody> 
              </table>
          </div>
        </div>
      </div>
    </div>
</div>
<div class="container">
  <div class="row">
    <div class="col-md-2 col-xs-6 offset-md-5 mb-3">
    
    <button type="submit" name="enroll" class="btn btn-warning mt-2">Enroll</button>
    </div>
  </div>
</div>
</form>
4

3 回答 3

0

将输入字段放在表单中并添加 enctype="multipart/form-data"

于 2021-04-10T13:17:14.987 回答
0
<select name="et[]"  class="form-select form-select-sm" aria-label=".form-select-sm example" multiple>

试试这个

在选择中添加多个

于 2021-04-10T21:14:46.027 回答
-1

解决了:

<?php
if (isset($_POST['enroll']))
{
if (!empty($_POST['chk1'])) 
{
  if (!empty($_POST['et'])) 
{
    $roll_no = $_SESSION['roll_no'];

    $selectbox1 = $_POST['et'];
    $selectbox11=implode(',',array_filter($selectbox1));
    
    
 
    
    $i=0; 
foreach($_POST['chk1'] as $checkbox1)
{

  $selectbox111 = array_diff(explode(",", $selectbox11),array(""));
  $selectbox1111  = $selectbox111[$i];

$values = explode("|" , $checkbox1);
$course_id= $values[0];
$semester= $values[1];
$course_name= $values[2];


$sql="INSERT INTO pendingcourse(roll_no,course_id,semester,course_name,exam_type,status) VALUES('$roll_no','$course_id','$semester','$course_name','$selectbox1111',0)";
$stmt = $connect->prepare($sql);
$stmt->execute();
$checkbox1='';
$i++;
}
}
}
}
?>
于 2021-05-21T09:38:41.533 回答