how to insert select option value in database using checkbox?When I click checkbox and click enroll button ,select option doesn’t inserting selected value.I have 3 options(Regular,Retake,Recourse) in “examtype” table but when I choose more than one subject select option not working perfectly. Here is my code… here is my select option image
<?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>
Advertisement
Answer
SOLVED:
<?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++; } } } } ?>