如何在PHP中使用复选框插入select选项中的选定值



如何使用复选框在数据库中插入select选项值?当我单击复选框并单击注册按钮时,选择选项不会插入选定的值。我有3个选项(常规,再扣,追索权(在";examtype";表,但当我选择多个主题时,选择选项并不完美。这是我的密码。。。这是我选择的图像

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

将输入字段放入表单中并添加enctype=";多部分/形式数据";

<select name="et[]"  class="form-select form-select-sm" aria-label=".form-select-sm example" multiple>

试试这个

在选择中添加多个

已解决:

<?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++;
}
}
}
}
?>

相关内容

  • 没有找到相关文章

最新更新